*/ class Age_Gate_Admin { /** * The options name to be used in this plugin * * @since 1.0.0 * @access private * @var string $option_name Option name of this plugin */ private $option_name = 'wp_age_gate'; /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of this plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; $this->settings = get_option($this->option_name . '_general'); $this->_updateCheck(); } /** * Register the stylesheets for the admin area. * * @since 1.0.0 */ public function enqueue_styles() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Age_Gate_Loader as all of the hooks are defined * in that particular class. * * The Age_Gate_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/age-gate-admin.css', array(), $this->version, 'all' ); } /** * Register the JavaScript for the admin area. * * @since 1.0.0 */ public function enqueue_scripts() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Age_Gate_Loader as all of the hooks are defined * in that particular class. * * The Age_Gate_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ global $pagenow; if( $pagenow == 'admin.php' && isset($_GET['page']) && $_GET['page'] === 'age-gate'){ wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/age-gate-admin.js', array( 'jquery', 'wp-color-picker' ), $this->version, false ); } } /** * Register plugin menu item * * @since 1.0.0 Displays the menu */ public function add_menu_section() { $this->plugin_page_hook_suffix = add_menu_page( __('Age Gate', $this->plugin_name), __('Age Gate', $this->plugin_name), 'edit_pages', $this->plugin_name, array($this, 'display_options_page'), 'dashicons-lock', 60 ); } /** * Create a settings link in the plugins screen * * @param mixed $links The standard links * @return mixed $links The links updated with our settings * @since 1.0.0 */ public function plugin_add_settings_link( $links ) { $settings_link = '' . __( 'Settings' ) . ''; array_unshift( $links, $settings_link ); return $links; } /** * Add manage_options * * In order to save as editor, they need this * @since 1.0.0 */ public function add_manage_options() { return 'edit_pages'; } /** * Add the option to restrict specific content * * @since 1.0.0 */ public function restrict_select_content() { ?>
/>
plugin_name .'-admin-display.php'; } /** * Register the settings sections * * @since 1.0.0 */ public function register_settings_sections() { $data = get_option($this->option_name . '_general'); /* General Settings */ add_settings_section( $this->option_name . '_general', __('Restriction settings', $this->plugin_name), array($this, $this->option_name . '_general_cb'), $this->plugin_name . '_general' ); /* General Settings */ add_settings_section( $this->option_name . '_style', __('Age gate style settings', $this->plugin_name), array($this, $this->option_name . '_general_cb'), $this->plugin_name . '_general' ); } /** * Register the individual settings * * @since 1.0.0 */ public function register_settings_section_global() { $data = get_option($this->option_name . '_general'); /* RESTRICTIONS */ add_settings_field( $this->option_name . '_min_age', __('Users must be', $this->plugin_name), array($this, $this->option_name . '_input_field_cb'), $this->plugin_name . '_general', $this->option_name . '_general', array( 'label_for' => $this->option_name . '_min_age', 'field_name' => '_min_age', 'type' => 'text', 'size' => 'small', 'setting' => $this->option_name . '_general', 'value' => (!isset($data[$this->option_name . '_min_age']) ? '' : $data[$this->option_name . '_min_age']), 'text' => __('years or older to view content', $this->plugin_name) ) ); add_settings_field( $this->option_name . '_restriction_type', __('Restrict', $this->plugin_name), array($this, $this->option_name . '_radio_field_cb'), $this->plugin_name . '_general', $this->option_name . '_general', array( 'value' => (!isset($data[$this->option_name . '_restriction_type']) ? '' : $data[$this->option_name . '_restriction_type']), 'label_for' => $this->option_name . '_restriction_type', 'field_name' => '_restriction_type', 'setting' => $this->option_name . '_general', 'options' => array('all' => __('All content', $this->plugin_name), 'selected' => __('Selected Content', $this->plugin_name)) ) ); add_settings_field( $this->option_name . '_restrict_register', __('Restrict registration', $this->plugin_name), array($this, (!get_option('users_can_register') ? $this->option_name . '_na_cb' : $this->option_name . '_checkbox_field_cb')), $this->plugin_name . '_general', $this->option_name . '_general', array( 'label_for' => $this->option_name . '_restrict_register', 'field_name' => '_restrict_register', 'setting' => $this->option_name . '_general', 'text' => __('Age check users during registering', $this->plugin_name), 'value' => (!isset($data[$this->option_name . '_restrict_register']) ? '' : $data[$this->option_name . '_restrict_register']), 'na' => __('Your site does not allow registration, so this option is not applicable', $this->plugin_name) ) ); add_settings_field( $this->option_name . '_input_type', __('Validate age using', $this->plugin_name), array($this, $this->option_name . '_select_field_cb'), $this->plugin_name . '_general', $this->option_name . '_general', array( 'label_for' => $this->option_name . '_input_type', 'field_name' => '_input_type', 'setting' => $this->option_name . '_general', 'value' => (!isset($data[$this->option_name . '_input_type']) ? '' : $data[$this->option_name . '_input_type']), 'options' => array('inputs' => __('Input fields', $this->plugin_name), 'selects' => __('Dropdown boxes', $this->plugin_name), 'buttons' => __('Yes/No', $this->plugin_name)) ) ); add_settings_field( $this->option_name . '_remember', __('Remember', $this->plugin_name), array($this, $this->option_name . '_checkbox_field_cb'), $this->plugin_name . '_general', $this->option_name . '_general', array( 'label_for' => $this->option_name . '_remember', 'field_name' => '_remember', 'setting' => $this->option_name . '_general', 'text' => __('Enable "remember me" checkbox', $this->plugin_name), 'value' => (!isset($data[$this->option_name . '_remember']) ? '' : $data[$this->option_name . '_remember']), ) ); add_settings_field( $this->option_name . '_date_format', __('Date format', $this->plugin_name), array($this, $this->option_name . '_select_field_cb'), $this->plugin_name . '_general', $this->option_name . '_general', array( 'label_for' => $this->option_name . '_date_format', 'field_name' => '_date_format', 'setting' => $this->option_name . '_general', 'value' => (!isset($data[$this->option_name . '_date_format']) ? '' : $data[$this->option_name . '_date_format']), 'options' => array('ddmmyyyy' => __('DD MM YYYY', $this->plugin_name), 'mmddyyyy' => __('MM DD YYYY', $this->plugin_name)) ) ); add_settings_field( $this->option_name . '_ignore_logged', __('Ignore logged in', $this->plugin_name), array($this, $this->option_name . '_checkbox_field_cb'), $this->plugin_name . '_general', $this->option_name . '_general', array( 'label_for' => $this->option_name . '_ignore_logged', 'field_name' => '_ignore_logged', 'setting' => $this->option_name . '_general', 'text' => __('Logged in users will not need to provide their age', $this->plugin_name), 'value' => (!isset($data[$this->option_name . '_ignore_logged']) ? '' : $data[$this->option_name . '_ignore_logged']), ) ); add_settings_field( $this->option_name . '_no_second_chance', __('No second chance', $this->plugin_name), array($this, $this->option_name . '_checkbox_field_cb'), $this->plugin_name . '_general', $this->option_name . '_general', array( 'label_for' => $this->option_name . '_no_second_chance', 'field_name' => '_no_second_chance', 'setting' => $this->option_name . '_general', 'text' => __('If someone fails the age test, they cannot try again until their next session', $this->plugin_name), 'value' => (!isset($data[$this->option_name . '_no_second_chance']) ? '' : $data[$this->option_name . '_no_second_chance']), ) ); /* STYLE */ add_settings_field( $this->option_name . '_logo', __('Logo', $this->plugin_name), array($this, $this->option_name . '_media_selector_cb'), $this->plugin_name . '_general', $this->option_name . '_style', array( 'value' => (!isset($data[$this->option_name . '_logo']) ? '' : $data[$this->option_name . '_logo']), 'label_for' => $this->option_name . '_logo', 'field_name' => '_logo', 'setting' => $this->option_name . '_general', ) ); add_settings_field( $this->option_name . '_button_text', __('Submit button text', $this->plugin_name), array($this, $this->option_name . '_input_field_cb'), $this->plugin_name . '_general', $this->option_name . '_style', array( 'label_for' => $this->option_name . '_button_text', 'field_name' => '_button_text', 'type' => 'text', 'size' => 'regular', 'setting' => $this->option_name . '_general', 'value' => (!isset($data[$this->option_name . '_button_text']) ? '' : $data[$this->option_name . '_button_text']), 'text' => '' ) ); add_settings_field( $this->option_name . '_instruction', __('Headline', $this->plugin_name), array($this, $this->option_name . '_input_field_cb'), $this->plugin_name . '_general', $this->option_name . '_style', array( 'label_for' => $this->option_name . '_instruction', 'field_name' => '_instruction', 'type' => 'text', 'size' => 'regular', 'setting' => $this->option_name . '_general', 'value' => (!isset($data[$this->option_name . '_instruction']) ? '' : $data[$this->option_name . '_instruction']), 'text' => __('Adding %s will output the minimum age', $this->plugin_name) ) ); add_settings_field( $this->option_name . '_messaging', __('Sub headline', $this->plugin_name), array($this, $this->option_name . '_input_field_cb'), $this->plugin_name . '_general', $this->option_name . '_style', array( 'label_for' => $this->option_name . '_messaging', 'field_name' => '_messaging', 'type' => 'text', 'size' => 'regular', 'setting' => $this->option_name . '_general', 'value' => (!isset($data[$this->option_name . '_messaging']) ? '' : $data[$this->option_name . '_messaging']), 'text' => __('Adding %s will output the minimum age', $this->plugin_name) ) ); add_settings_field( $this->option_name . '_background_colour', __('Background colour', $this->plugin_name), array($this, $this->option_name . '_colour_field_cb'), $this->plugin_name . '_general', $this->option_name . '_style', array( 'label_for' => $this->option_name . '_background_colour', 'field_name' => '_background_colour', 'size' => 'regular', 'setting' => $this->option_name . '_general', 'value' => (!isset($data[$this->option_name . '_background_colour']) ? '' : $data[$this->option_name . '_background_colour']), 'text' => '' ) ); add_settings_field( $this->option_name . '_background_image', __('Background image', $this->plugin_name), array($this, $this->option_name . '_media_selector_cb'), $this->plugin_name . '_general', $this->option_name . '_style', array( 'value' => (!isset($data[$this->option_name . '_background_image']) ? '' : $data[$this->option_name . '_background_image']), 'label_for' => $this->option_name . '_background_image', 'field_name' => '_background_image', 'setting' => $this->option_name . '_general', ) ); add_settings_field( $this->option_name . '_foreground_colour', __('Foreground colour', $this->plugin_name), array($this, $this->option_name . '_colour_field_cb'), $this->plugin_name . '_general', $this->option_name . '_style', array( 'label_for' => $this->option_name . '_foreground_colour', 'field_name' => '_foreground_colour', 'size' => 'regular', 'setting' => $this->option_name . '_general', 'value' => (!isset($data[$this->option_name . '_foreground_colour']) ? '' : $data[$this->option_name . '_foreground_colour']), 'text' => '' ) ); add_settings_field( $this->option_name . '_text_colour', __('Text colour', $this->plugin_name), array($this, $this->option_name . '_colour_field_cb'), $this->plugin_name . '_general', $this->option_name . '_style', array( 'label_for' => $this->option_name . '_text_colour', 'field_name' => '_text_colour', 'size' => 'regular', 'setting' => $this->option_name . '_general', 'value' => (!isset($data[$this->option_name . '_text_colour']) ? '' : $data[$this->option_name . '_text_colour']), 'text' => '' ) ); add_settings_field( $this->option_name . '_styling', __('Layout', $this->plugin_name), array($this, $this->option_name . '_checkbox_field_cb'), $this->plugin_name . '_general', $this->option_name . '_style', array( 'label_for' => $this->option_name . '_styling', 'field_name' => '_styling', 'setting' => $this->option_name . '_general', 'text' => __('Use plugin style on the front end', $this->plugin_name), 'value' => (!isset($data[$this->option_name . '_styling']) ? '' : $data[$this->option_name . '_styling']), ) ); add_settings_field( $this->option_name . '_additional', __('Additional content', $this->plugin_name), array($this, $this->option_name . '_rich_text_cb'), $this->plugin_name . '_general', $this->option_name . '_style', array( 'label_for' => $this->option_name . '_additional', 'field_name' => '_additional', 'setting' => $this->option_name . '_general', 'text' => __('Use plugin style on the front end', $this->plugin_name), 'value' => (!isset($data[$this->option_name . '_additional']) ? '' : $data[$this->option_name . '_additional']), ) ); register_setting( $this->plugin_name . '_general', $this->option_name . '_general', array($this, 'sanitize') ); } /** * Sanitize * @param mixed $input The post data * @return mixed $sanitized The sanitized data * @since 1.0.0 */ public function sanitize($input) { $sanitized = array(); foreach ($input as $option => $value) { switch($option){ case 'wp_age_gate_min_age': case 'wp_age_gate_restrict_register': case 'wp_age_gate_remember': case 'wp_age_gate_ignore_logged': case 'wp_age_gate_logo': case 'wp_age_gate_background_image': case 'wp_age_gate_styling': case 'wp_age_gate_no_second_chance': $sanitized[$option] = intval($value); break; case 'wp_age_gate_restriction_type': case 'wp_age_gate_input_type': case 'wp_age_gate_date_format': case 'wp_age_gate_button_text': case 'wp_age_gate_instruction': case 'wp_age_gate_messaging': case 'wp_age_gate_background_colour': case 'wp_age_gate_foreground_colour': case 'wp_age_gate_text_colour': $sanitized[$option] = esc_html(strval($value)); break; case 'wp_age_gate_additional': $sanitized[$option] = $value; break; } } return $sanitized; } /** * Callback to display input field * @param mixed $args * @since 1.0.0 */ public function wp_age_gate_input_field_cb($args) { echo ' ' . $args['text']; } /** * Callback to display colour picker field * @param mixed $args * @since 1.0.0 */ public function wp_age_gate_colour_field_cb($args) { echo ' ' . $args['text']; } /** * Callback to display checkbox field * @param mixed $args * @since 1.0.0 */ public function wp_age_gate_checkbox_field_cb($args) { echo ''; } /** * Callback to display text when setting is not applicable * @param mixed $args * @since 1.0.0 */ public function wp_age_gate_na_cb($args) { echo $args['na']; } /** * Callback to display radio button field * @param mixed $args * @since 1.0.0 */ public function wp_age_gate_radio_field_cb($args) { echo '
' . __('Select', $this->plugin_name) . ''; foreach ($args['options'] as $key => $value) { echo '
'; } echo "
"; } /** * Callback to display select field * @param mixed $args * @since 1.0.0 */ public function wp_age_gate_select_field_cb($args) { echo '' . $args['text']; } /** * Callback to display rich text area * @param mixed $args * @since 1.0.0 */ public function wp_age_gate_rich_text_cb($args) { echo "

" . __('Use this area to add an addtional info or terms of entry.', $this->plugin_name) . '

'; echo '
'; wp_editor( $args['value'], $this->option_name . $args['field_name'], array( 'media_buttons' => false, 'quicktags' => false, 'tinymce' => array('wp_autoresize_on' => false, 'resize' => false, 'statusbar' => false, 'mce_buttons' => 'bold, italic'), 'textarea_name' => $args['setting'] . '[' . $this->option_name . $args['field_name'] . ']' ) ); echo '
'; } /** * Render the text for the general section * * @since 1.0.0 */ public function wp_age_gate_general_cb($args) { switch($args['id']){ case $this->option_name . '_style': echo '

' . __( 'Update the look of the age gate.', $this->plugin_name ) . '

'; break; default: echo '

' . __( 'Please change the settings accordingly.', $this->plugin_name ) . '

'; } } /** * Callback to display media selector * @param mixed $args * @since 1.0.0 */ public function wp_age_gate_media_selector_cb($args) { wp_enqueue_media(); ?>
' style="max-height: 100px">
plugin_name) . '" data-option="'. $this->option_name . $args['field_name'] .'" />'; } } /** * Callback to display remove fields from tinymce * @param mixed $args * @since 1.0.0 */ public function wp_age_gate_customise_tinymce($buttons) { $removeButtons = array('formatselect','blockquote','alignleft','aligncenter','alignright','wp_more','fullscreen','wp_adv'); foreach ($buttons as $button_key => $button_value) { if( in_array($button_value, $removeButtons )){ unset($buttons[$button_key]); } } return $buttons; } /** * Checks the plugin version against the stored version * and updates the settings if mismatched * * @since 1.1.0 * */ private function _updateCheck() { if ($this->version !== get_option('wp_age_gate_version')){ require_once AGE_GATE_DIR . 'includes/class-age-gate-activator.php'; Age_Gate_Activator::activate(); update_option('wp_age_gate_version', $this->version); } } }