%s could not be restored: probably it is not writable.', NICER_PERMALINKS_ID ), '.htaccess' ), 'error' );
return;
}
// Save plugin status in db
bb_update_option( 'nicer_permalinks_enabled', 0 );
} else { // Try to enable the plugin
if ( false === backup_htaccess() ) { // Generate error notice and return if errors occurred
$notices[] = array( sprintf( __( '%1$s backup could not be generated: probably there is no %2$s in bbPress root folder or it is not writable.', NICER_PERMALINKS_ID ), '.htaccess', 'htaccess.bak' ), 'error' );
return;
}
if ( false === file_put_contents( BB_PATH . '.htaccess', get_nicer_htaccess() ) ) { // Generate error notice and return if errors occurred
$notices[] = array( sprintf( __( '%s could not be updated: probably it is not writable.', NICER_PERMALINKS_ID ), '.htaccess' ), 'error' );
return;
}
// Save plugin status in db
bb_update_option( 'nicer_permalinks_enabled', 1 );
}
} else ; // No need to process request if requested status matches plugin current status
// Generate notice
$notices[] = array( sprintf( __( '%s settings updated.', NICER_PERMALINKS_ID ), NICER_PERMALINKS_NAME ), '' );
}
}
/**
* Returns plugin configuration errors or false if no error is found
*
* @param int $status Plugin status
*
* @uses nicer_permalinks_enabled()
* @uses name_based_permalinks_enabled()
* @uses bb_get_uri()
*
* @return array|boolean Errors|false
*/
function nicer_permalinks_config_errors( $status ) {
if ( is_null( $status ) ) // Retrieve plugin status if no param is passed
$status = nicer_permalinks_enabled();
$errors = array();
if ( 0 == $status ) { // Check if plugin can be enabled
if ( !name_based_permalinks_enabled() ) // Generate error if name based permalinks are disabled
$errors[] = sprintf( __( 'Name based permalinks are not enabled.', NICER_PERMALINKS_ID ), bb_get_uri( 'bb-admin/options-permalinks.php' ) );
if ( !is_writable( BB_PATH . '.htaccess' ) ) // Generate error if .htaccess is not writable
$errors[] = sprintf( __( '%s is not writable.', NICER_PERMALINKS_ID ), '.htaccess' );
if ( !file_exists( BB_PATH . 'htaccess.bak' ) ) // Generate error if there is no htaccess.bak in bbPress root folder
$errors[] = sprintf( __( 'There is no %s in bbPress root folder.', NICER_PERMALINKS_ID ), 'htaccess.bak' );
else
if ( !is_writable( BB_PATH . 'htaccess.bak' ) ) // Generate error if htaccess.bak is not writable
$errors[] = sprintf( __( '%s is not writable.', NICER_PERMALINKS_ID ), 'htaccess.bak' );
} elseif ( 1 == $status ) { // Check if plugin can be disabled
if ( !is_writable( BB_PATH . '.htaccess' ) ) // Generate error if .htaccess is not writable
$errors[] = sprintf( __( '%s is not writable.', NICER_PERMALINKS_ID ), '.htaccess' );
} else { // Generate error if $status does not match any previous case (should never happen)
$errors[] = sprintf( __( '%s is not a valid plugin status.', NICER_PERMALINKS_ID ), $status );
}
// Return array of errors or false if no error occurred
return ( count( $errors ) ) ? $errors : false;
}
/**
* Check if name based permalinks are enabled
*
* @uses bb_get_option()
*
* @return boolean
*/
function name_based_permalinks_enabled() {
$mod_rewrite = bb_get_option( 'mod_rewrite' );
if ( is_null( $mod_rewrite ) )
$mod_rewrite = 0;
return ( 'slugs' == $mod_rewrite ) ? true : false;
}
/**
* Generate nicer .htaccess
*
* @uses bb_get_option()
*
* @return string
*/
function get_nicer_htaccess() {
$nicer_htaccess = <<