version ); wp_enqueue_script( '2048_settings', plugins_url( 'js/settings.js' , dirname(__FILE__) ), array('wp-color-picker','thickbox','media-upload'), $this->version , true ); wp_localize_script( '2048_settings', 'wp2048', array( 'ajaxurl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('wp2048_ajax'), ) ); } /** * Add settings menu and page */ public function settings_menu() { // This page will be under "Settings" $page = add_options_page( 'Settings Admin', '2048 Number Game', 'manage_options', 'wp2048', array( $this, 'settings_page' ) ); add_action( 'admin_print_styles-' . $page, array( $this, 'settings_enqueue' ) ); } /** * Settings page callback */ public function settings_page() { // Set the active tabs $active_tab = ( isset($_GET['tab']) ) ? $_GET['tab'] : ''; ?>
'customization', 'label' => 'Checking this will make default shortcode to use customizations' ) ); add_settings_field( 'hide_howto', // ID 'Hide How-To', // Title array( $this, 'checkbox_callback' ), // Callback 'wp2048_options_page', // Page 'wp2048_options_general', // Section array( 'field' => 'hide_howto', 'label' => 'Hide game instructions below game board' ) ); add_settings_field( 'guest_highscore', // ID 'Guest High Score', // Title array( $this, 'checkbox_callback' ), // Callback 'wp2048_options_page', // Page 'wp2048_options_general', // Section array( 'field' => 'guest_highscore', 'label' => 'Allow non logged-in user to submit high score' ) ); add_settings_field( 'notify', // ID 'Notify High Score', // Title array( $this, 'notify_callback' ), // Callback 'wp2048_options_page', // Page 'wp2048_options_general' // Section ); // Email Templates add_settings_section( 'wp2048_options_email', // ID 'Email Templates', // Title array( $this, 'print_section_email' ), // Callback 'wp2048_options_page' // Page ); add_settings_field( 'highscore_new', 'New High Score', array( $this, 'email_template_callback' ), 'wp2048_options_page', 'wp2048_options_email', array( 'field' => 'highscore_new', 'desc' => 'Congratulates on achiving site\'s new high score.' ) ); add_settings_field( 'highscore_lost', 'Lost High Score', array( $this, 'email_template_callback' ), 'wp2048_options_page', 'wp2048_options_email', array( 'field' => 'highscore_lost', 'desc' => 'Sent to previous user their high score just broken' ) ); // Advanced Options add_settings_section( 'wp2048_options_advanced', // ID 'Advanced Options', // Title array( $this, 'print_section_advanced' ), // Callback 'wp2048_options_page' // Page ); add_settings_field( 'tx_viewport', 'Meta Viewport', array( $this, 'text_callback' ), 'wp2048_options_page', 'wp2048_options_advanced', array( 'field' => 'tx_viewport', 'desc' => 'Only applies to page with [2048] shortcode. If left blank, it will not modify (or add into) your theme\'s viewport.' ) ); add_settings_field( 'metahead', // ID 'Disable Mobile Friendly', // Title array( $this, 'checkbox_callback' ), // Callback 'wp2048_options_page', // Page 'wp2048_options_advanced', // Section array( 'field' => 'metahead', 'label' => 'Do not add meta for mobile friendly. Only applies to page with [2048] shortcode.' ) ); add_settings_field( 'delete', // ID 'Delete Plugin Data', // Title array( $this, 'checkbox_callback' ), // Callback 'wp2048_options_page', // Page 'wp2048_options_advanced', // Section array( 'field' => 'delete', 'label' => 'Remove all plugin data upon de-activation' ) ); /** * Customizations Tab */ register_setting( 'wp2048_customize_settings', 'wp2048_custom', array( $this, 'sanitize_customize' ) ); add_settings_section( 'wp2048_customize_section', // ID '', // Title array( $this, 'print_section_customize' ), // Callback 'wp2048_customize_page' // Page ); add_settings_field( 'feature', // ID 'Enabled Features', // Title array( $this, 'features_callback' ), // Callback 'wp2048_customize_page', // Page 'wp2048_customize_section' // Section ); add_settings_field( 'color_font', // ID 'Font Color', // Title array( $this, 'colorpicker_callback' ), // Callback 'wp2048_customize_page', // Page 'wp2048_customize_section', // Section array( 'type' => 'font' ) ); add_settings_field( 'color_bg', // ID 'Background Color', // Title array( $this, 'colorpicker_callback' ), // Callback 'wp2048_customize_page', // Page 'wp2048_customize_section', // Section array( 'type' => 'bg' ) ); add_settings_field( 'color_grid', // ID 'Grid Color', // Title array( $this, 'colorpicker_callback' ), // Callback 'wp2048_customize_page', // Page 'wp2048_customize_section', // Section array( 'type' => 'grid' ) ); foreach($this->tiles as $tile) { add_settings_field( $tile, 'Tile '.$tile, array( $this, 'customize_callback' ), 'wp2048_customize_page', 'wp2048_customize_section', array( 'tile' => $tile ) ); } add_settings_section( 'wp2048_custom_shortcode', // ID '', // Title array( $this, 'print_section_shortcode' ), // Callback 'wp2048_customize_page' // Page ); } /** * Sanitize each setting field as needed * * @param array $input Contains all settings fields as array keys */ public function sanitize( $input ) { $new_input = array(); $blank_subject = false; foreach ($input as $field => $value) { if ($field == 'email_template') { $email_template = $value; foreach ($email_template as $etype => $template) { if ( !empty($template['subject']) ) { $sanitized_template[$etype]['subject'] = sanitize_text_field($template['subject']); } else { $sanitized_template[$etype]['subject'] = $this->option['email_template'][$etype]['subject']; $blank_subject = true; } $sanitized_template[$etype]['message'] = isset($template['message']) ? esc_textarea($template['message']) : ''; } $new_input[$field] = $sanitized_template; } else { $exp = explode('_',$field); if ($exp[0] == 'ta') { $new_input[$field] = esc_textarea($value); } elseif ($exp[0] == 'tx') { $new_input[$field] = sanitize_text_field($value); } else { $new_input[$field] = absint($value); } } } // email subject cannot be blank if ($blank_subject) { add_settings_error( 'wp2048_blanksubject', esc_attr( 'settings_updated' ), __('Email subject cannot be left blank. Affected field(s) reverted to previous setting.','wp2048'), 'error' ); } return $new_input; } /** * Sanitize function for Customizer */ public function sanitize_customize( $input ) { $new_input = array(); $features = $input['feature']; foreach($features as $type => $feature) { $new_input['feature'][$type] = absint($feature); } // Custom Text Sanitization $texts = $input['text']; foreach ($texts as $tkey => $text) { if ( empty($text) ) { //fill-in blank input with defaults $new_input['text'][$tkey] = (string)$tkey; $partial = true; } else { $new_input['text'][$tkey] = sanitize_text_field($text); } } // Custom Font Size $error_size = false; $sizes = $input['size']; foreach ($sizes as $skey => $size) { $new_input['size'][$skey] = absint($size); if ( !empty($size) && !is_numeric($size) ) { $error_size = true; } } if ($error_size) { add_settings_error( 'wp2048_fontsize', esc_attr( 'settings_updated' ), __('Font size accepts positive integer only. Invalid input has been discarded.','wp2048'), 'error' ); } // Image uploads $images = $input['image']; foreach ($images as $ikey => $image){ if ( empty($image) ) { //fill-in blank input with defaults $new_input['image'][$ikey] = 0; } else { // allow only jpg, png and gif images $new_input['image'][$ikey] = in_array(strtolower(end(explode('.', $image))), array('jpg', 'jpeg', 'png', 'gif')) ? $image : ''; } } // Custom Colors $error_color = array(); $colors = $input['color']; foreach ($colors as $ckey => $color) { if ( !empty($color) ) { $color = sanitize_text_field($color); if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color) ) { $new_input['color'][$ckey] = $color; } else { $error_color[] = $ckey; } } } if ( !empty($error_color) ) { $error_color = explode(", ",$error_color); //TODO add_settings_error( 'wp2048_hexcolor', esc_attr( 'settings_updated' ), __('Some color fields contains invalid HEX values. Invalid input has been discarded.','wp2048'), 'error' ); } return $new_input; } /** * Function to sanitize color picker */ private function sanitize_colorpicker( $input ) { $output = array(); $error = false; foreach ($input as $key => $color) { if ( !empty($color) ) { $color = sanitize_text_field($color); if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color) ) { $output['color'][$key] = $color; } else { $error = true; } } } return array( 'output' => $output, 'error' => $error ); } /** * Print the Section text */ public function print_section_general() { // Some know-how echo 'To add 2048 Number Game on your WordPress site, simply use [2048] shortcode into any page or post. You can also add the shortcode using the button on post editor when you are creating or updating a page or post. Configure the default settings on this page.
%%SCORE%% in the email template below to replace with the new high score.';
}
public function print_section_advanced()
{
echo ''; } public function print_section_customize() { echo '
This settings page is for default values when you are using shortcode [2048 custom=1] or when you have set to default the game to use the custom features on the settings tab. You able to customize various cosmetic elements of the game. For any fields you left blank or did not check the box(es) of enabled features, that particular option will fall back into the original 2048 appearance.
By using the shortcode [2048 custom=1] on any page or post, it will display the game based on the saved values on this page. But that only gives you two options, either the original 2048 design or the customized settings here. The generate button above provides you unlimited 2048 game designs by generating custom shortcodes.
Default color is %2$s
', $default[$colortype], $default[$colortype] ); } public function features_callback() { printf( '', isset($this->custom['feature']['text']) ? checked( 1, $this->custom['feature']['text'], false ) : '' ); printf( ''.__('Disabled features will fall back to default values in the original 2048','wp2048').'
'; } public function customize_callback($args) { $tile = $args['tile']; // Custom Text printf( '', $tile, ( !empty($this->custom['text'][$tile]) && $this->custom['text'][$tile] != $tile ) ? esc_attr( $this->custom['text'][$tile] ) : '' ); // Font Size printf( ' px', $tile, !empty( $this->custom['size'][$tile] ) ? esc_attr( $this->custom['size'][$tile] ) : '' ); // Image Upload $img = !empty( $this->custom['image'][$tile] ) ? esc_attr( $this->custom['image'][$tile] ) : ''; printf( 'Default color is %2$s
', $default[$ckey], $default[$ckey] ); } /** * Displays the Preview of 2048 Game Board */ private function print_customize_preview() { print '