anylinkOptions = get_option( 'anylink_options' );
}
public function addMenu() {
$al_option_page = add_submenu_page( 'options-general.php', __( 'anylink Settings', 'anylink' ), __( 'anylink Settings', 'anylink' ), 'manage_options', 'anyLinkSetting', array( &$this, 'anyLinkSettingPage' ) );
add_action( 'admin_print_scripts-' . $al_option_page, array( &$this, 'anylinkAdminScripts' ) );
add_action( 'admin_print_styles-' . $al_option_page, array( &$this, 'anylinkAdminScripts' ) );
}
//output scripts and styles
public function anylinkAdminScripts() {
wp_enqueue_script( 'anylink_script' );
wp_enqueue_style( 'anylink_style' );
}
public function anyLinkSettingPage() {
if( ! current_user_can( 'manage_options' ) )
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
include_once( ANYLNK_PATH . '/al_setting.php' );
}
public function alAdminInit() {
//register a javascript and css files to output later
wp_register_script( 'anylink_script', plugins_url( '/images/al_script.js', dirname( __FILE__ ) ) );
wp_register_style( 'anylink_style', plugins_url( '/images/al_style.css', dirname( __FILE__ ) ) );
register_setting( 'anylink_options_group', 'anylink_options', array( &$this, 'alCatValidate' ) );
add_settings_section( 'al_general_settings', __( 'General Settings', 'anylink'), array( &$this, 'alGeneralDisp' ), 'anyLinkSetting' );
add_settings_field( 'al_redirect_cat', __( 'Redirect catalog', 'anylink' ), array( &$this, 'dispRedirectCat' ), 'anyLinkSetting', 'al_general_settings' );
add_settings_field( 'al_redirect_type', __( 'Redirect HTTP code', 'anylink'), array( &$this, 'dispRedirctType' ), 'anyLinkSetting', 'al_general_settings' );
add_settings_field( 'al_slug_num', __( 'Length of slugs', 'anylink'), array( &$this, 'dispSlugNum' ), 'anyLinkSetting', 'al_general_settings' );
add_settings_field( 'al_slug_char', __( 'Component of slug', 'anylink' ), array( &$this, 'dispSlugChar' ), 'anyLinkSetting', 'al_general_settings' );
add_settings_field( 'al_form_identify', '', array( &$this, 'hiddenFormIdentify' ), 'anyLinkSetting', 'al_general_settings' );
load_plugin_textdomain( 'anylink', false, ANYLNK_PATH . '/i18n/' );
}
public function alGeneralDisp() {
echo "";
}
public function dispRedirectCat() {
$cat = $this -> anylinkOptions['redirectCat'];
echo site_url() . "//ab12
" . __( 'Make sure it starts with a letter, ONLY contains letters, numbers, underscore and dash. The max. length is 12', 'anylink' );
}
public function dispRedirctType() {
$type = ( int )$this -> anylinkOptions['redirectType'];
//determine which radio button should be checked
$checked301 = ' ';
$checked307 = ' ';
$checked200 = ' ';
switch( $type ) {
case 301:
$checked301 = 'checked="checked"';
break;
case 307:
$checked307 = 'checked="checked"';
break;
case 200;
$checked200 = 'checked="checked"';
break;
}
$redirectType = "";
$redirectType .= "
";
$redirectType .= "";
$redirectType .= "
";
$redirectType .= "";
$redirectType .= "
";
echo $redirectType;
}
public function dispSlugNum() {
$num = $this ->anylinkOptions['slugNum'];
echo "
No less than 4 and no more than 12";
}
public function dispSlugChar() {
$chars = $this -> anylinkOptions['slugChar'];
$htmlChar = "
";
$htmlChar .= "
";
$htmlChar .= "";
$htmlChar .= "
" . __( 'Recommended setting is 4 digits and alphabets. If using PURE DIGITS please set the length no less than 6.', 'anylink' ) . "";
echo $htmlChar;
}
/* I should put some validations here
* a filter named "sanitize_option_$optionname" is applied when you can update_option
* so we need an identify key to determine which form the data come form
*/
public function alCatValidate( $input ) {
if( ! array_key_exists( 'identify', $input ) )
return $input;
$oldOptions = $this -> anylinkOptions;
$input = array_map( "trim", $input );
if( preg_match( '/^[a-z][a-z0-9_-]{0,11}/', $input['redirectCat'] ) )
$oldOptions['redirectCat'] = $input['redirectCat'];
if( $input['redirectType'] == 301 || $input['redirectType'] == 307 || $input['redirectType'] == 200 )
$oldOptions['redirectType'] = $input['redirectType'];
if( is_int( ( int )$input['slugNum'] ) && $input['slugNum'] < 13 && $input['slugNum'] > 3 )
$oldOptions['slugNum'] = $input['slugNum'];
$oldOptions['slugChar'] = $input['slugChar'];
return $oldOptions;
}
//out put a hidden field to identify the form
public function hiddenFormIdentify() {
$hiddenHtml = "";
echo $hiddenHtml;
}
//flush the rewrite rules
public function flushRules() {
$alOptions = get_option( 'anylink_options' );
if( $alOptions['redirectCat'] != $alOptions['oldCat'] || $alOptions['redirectType'] != $alOptions['oldRedirectType'] ) {
$cat = $alOptions['redirectCat'];
$type = $alOptions['redirectType'];
global $wp_rewrite;
if( $type == '200' ) {
/* get the relative path of redirect file */
$redirectFile = substr( plugins_url(), strlen( home_url() ) + 1 ) . '/' . ANYLNK_PLUGIN . '/redirect.php';
/* add an external rewrite rule which will be written into .htaccess file */
$wp_rewrite -> add_external_rule( "$cat/([0-9a-z]{4,})", $redirectFile . '?slug=$1' );
flush_rewrite_rules();
} else {
$wp_rewrite -> flush_rules( true );
add_rewrite_rule( "$cat/([0-9a-z]{4,})", 'index.php?' . $cat . '=$matches[1]', 'top' );
flush_rewrite_rules();
}
$alOptions['oldCat'] = $alOptions['redirectCat'];
$alOptions['oldRedirectType'] = $alOptions['redirectType'];
update_option( 'anylink_options', $alOptions );
}
}
}
?>