[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-forums/bbpress/bb-admin/ -> options-permalinks.php (source)

   1  <?php
   2  
   3  require_once ('admin.php');
   4  
   5  $file_source = BB_PATH . 'bb-admin/includes/defaults.bb-htaccess.php';
   6  $file_target = BB_PATH . '.htaccess';
   7  include( $file_source );
   8  $file_source_rules = $_rules; // This is a string

   9  
  10  if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) && $_POST['action'] == 'update') {
  11  
  12      bb_check_admin_referer( 'options-permalinks-update' );
  13  
  14      foreach ( (array) $_POST as $option => $value ) {
  15          if ( !in_array( $option, array('_wpnonce', '_wp_http_referer', 'action', 'submit') ) ) {
  16              $option = trim( $option );
  17              $value = is_array( $value ) ? $value : trim( $value );
  18              $value = stripslashes_deep( $value );
  19              if ( $value ) {
  20                  bb_update_option( $option, $value );
  21              } else {
  22                  bb_delete_option( $option );
  23              }
  24          }
  25      }
  26  
  27      $mod_rewrite = (string) bb_get_option( 'mod_rewrite' );
  28  
  29      $goback = remove_query_arg( array( 'updated', 'notapache', 'notmodrewrite' ), wp_get_referer() );
  30  
  31      // Make sure mod_rewrite is possible on the server

  32      if ( !$is_apache ) {
  33          bb_delete_option( 'mod_rewrite_writable' );
  34          $goback = add_query_arg( 'notapache', 'true', $goback );
  35          bb_safe_redirect( $goback );
  36          exit;
  37      } elseif ( '0' !== $mod_rewrite && !apache_mod_loaded( 'mod_rewrite', true ) ) {
  38          bb_delete_option( 'mod_rewrite_writable' );
  39          bb_update_option( 'mod_rewrite', '0' );
  40          $goback = add_query_arg( 'notmodrewrite', 'true', $goback );
  41          bb_safe_redirect( $goback );
  42          exit;
  43      }
  44  
  45      $file_target_rules = array();
  46  
  47      $file_target_exists = false;
  48      $file_target_writable = true;
  49      if ( file_exists( $file_target ) ) {
  50          if ( is_readable( $file_target ) ) {
  51              $file_target_rules = explode( "\n", implode( '', file(  $file_target ) ) );
  52          }
  53          $file_target_exists = true;
  54          if ( !is_writable( $file_target ) ) {
  55              $file_target_writable = false;
  56          }
  57      } else {
  58          $file_target_dir = dirname( $file_target );
  59          if ( file_exists( $file_target_dir ) ) {
  60              if ( !is_writable( $file_target_dir ) || !is_dir( $file_target_dir ) ) {
  61                  $file_target_writable = false;
  62              }
  63          } else {
  64              $file_target_writable = false;
  65          }
  66      }
  67  
  68      // Strip out existing bbPress rules

  69      $_keep_rule = true;
  70      $_kept_rules = array();
  71      foreach ( $file_target_rules as $_rule ) {
  72          if ( false !== strpos( $_rule, '# BEGIN bbPress' ) ) {
  73              $_keep_rule = false;
  74              continue;
  75          } elseif ( false !== strpos( $_rule, '# END bbPress' ) ) {
  76              $_keep_rule = true;
  77              continue;
  78          }
  79          if ( $_keep_rule ) {
  80              $_kept_rules[] = $_rule;
  81          }
  82      }
  83  
  84      $file_target_rules = join( "\n", $_kept_rules ) . "\n" . $file_source_rules;
  85      
  86      $file_target_written = 0;
  87      if ( $file_target_writable ) {
  88          // Open the file for writing - rewrites the whole file

  89          if ( $file_target_handle = fopen( $file_target, 'w' ) ) {
  90              if ( fwrite( $file_target_handle, $file_target_rules ) ) {
  91                  $file_target_written = 1;
  92              }
  93              // Close the file

  94              fclose( $file_target_handle );
  95              @chmod( $file_target, 0666 );
  96          }
  97      }
  98  
  99      bb_update_option( 'mod_rewrite_writable', $file_target_writable );
 100      $goback = add_query_arg( 'updated', 'true', $goback );
 101      bb_safe_redirect( $goback );
 102      exit;
 103  }
 104  
 105  if ( $is_apache && bb_get_option( 'mod_rewrite' ) && !bb_get_option( 'mod_rewrite_writable' ) ) {
 106      $manual_instructions = true;
 107  }
 108  
 109  if ( !empty( $_GET['notmodrewrite'] ) ) {
 110      $manual_instructions = false;
 111      bb_admin_notice( __( '<strong>It appears that your server does not support custom permalink structures.</strong>' ), 'error' );
 112  }
 113  
 114  if ( !empty( $_GET['notapache'] ) ) {
 115      $manual_instructions = false;
 116      bb_admin_notice( __( '<strong>Rewriting on webservers other than Apache using mod_rewrite is currently unsupported, but we won&#8217;t stop you from trying.</strong>' ), 'error' );
 117  }
 118  
 119  if ( !empty( $_GET['updated'] ) ) {
 120      if ( $manual_instructions ) {
 121          bb_admin_notice( __( '<strong>You should update your .htaccess now.</strong>' ) );
 122      } else {
 123          bb_admin_notice( __( '<strong>Permalink structure updated.</strong>' ) );
 124      }
 125  }
 126  
 127  $permalink_options = array(
 128      'mod_rewrite' => array(
 129          'title' => __( 'Permalink type' ),
 130          'type' => 'radio',
 131          'options' => array(
 132              '0' => sprintf( __( '<span>None</span> <code>%s</code>' ), bb_get_uri( 'forum.php', array( 'id' => 1 ), BB_URI_CONTEXT_TEXT ) ),
 133              '1' => sprintf( __( '<span>Numeric</span> <code>%s</code>' ), bb_get_uri( 'forum/1', null, BB_URI_CONTEXT_TEXT ) ),
 134              'slugs' => sprintf( __( '<span>Name based</span> <code>%s</code>' ), bb_get_uri( '/forum/first-forum', null, BB_URI_CONTEXT_TEXT ) )
 135          )
 136      )
 137  );
 138  
 139  $bb_admin_body_class = ' bb-admin-settings';
 140  
 141  bb_get_admin_header();
 142  
 143  ?>
 144  
 145  <div class="wrap">
 146  
 147  <h2><?php _e( 'Permalink Settings' ); ?></h2>
 148  <?php do_action( 'bb_admin_notices' ); ?>
 149  
 150  <form class="settings" method="post" action="<?php bb_uri( 'bb-admin/options-permalinks.php', null, BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN ); ?>">
 151      <fieldset>
 152          <p>
 153              <?php _e( 'By default bbPress uses web URLs which have question marks and lots of numbers in them, however bbPress offers you the ability to choose an alternative URL structure for your permalinks. This can improve the aesthetics, usability, and forward-compatibility of your links.' ); ?>
 154          </p>
 155  <?php
 156  foreach ( $permalink_options as $option => $args ) { 
 157      bb_option_form_element( $option, $args );
 158  }
 159  ?>
 160      </fieldset>
 161      <fieldset class="submit">
 162          <?php bb_nonce_field( 'options-permalinks-update' ); ?>
 163          <input type="hidden" name="action" value="update" />
 164          <input class="submit" type="submit" name="submit" value="<?php _e('Save Changes') ?>" />
 165      </fieldset>
 166  </form>
 167  
 168  <?php
 169  if ( $manual_instructions ) {
 170  ?>
 171  <form class="settings" method="post" action="<?php bb_uri( 'bb-admin/options-permalinks.php', null, BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN ); ?>">
 172      <fieldset>
 173          <p>
 174              <?php _e( 'If your <code>.htaccess</code> file were <a href="http://codex.wordpress.org/Changing_File_Permissions">writable</a>, we could do this automatically, but it isn&#8217;t so these are the mod_rewrite rules you should have in your <code>.htaccess</code> file. Click in the field and press <kbd>CTRL + a</kbd> to select all.' ); ?>
 175          </p>
 176          <textarea dir="ltr" id="rewrite-rules" class="readonly" readonly="readonly" rows="6"><?php echo esc_html( trim( $file_source_rules ) ); ?></textarea>
 177      </fieldset>
 178  </form>
 179  
 180  <?php
 181  }
 182  ?>
 183  
 184  </div>
 185  
 186  <?php
 187  
 188  bb_get_admin_footer();


Generated: Thu Dec 7 01:01:35 2017 Cross-referenced by PHPXref 0.7.1