if (!defined('ABSPATH')) { exit; } // Exit if accessed directly /** * Returns the main instance of Starter_Plugin to prevent the need to use globals. * * @since 1.0.0 * @return object Starter_Plugin */ function Starter_Plugin() { return Starter_Plugin::instance(); } // End Starter_Plugin() Starter_Plugin(); /** * Main Starter_Plugin Class * * @class Starter_Plugin * @version 1.0.0 * @since 1.0.0 * @package Starter_Plugin * @author Matty */ final class Starter_Plugin { /** * Starter_Plugin The single instance of Starter_Plugin. * @var object * @access private
/** * Render a field of a given type. * @access public * @since 1.0.0 * @param array $args The field parameters. * @return void */ public function render_field($args) { $html = ''; if (!in_array($args['type'], $this->get_supported_fields())) { return ''; } // Supported field type sanity check. // Make sure we have some kind of default, if the key isn't set. if (!isset($args['default'])) { $args['default'] = ''; } $method = 'render_field_' . $args['type']; if (!method_exists($this, $method)) { $method = 'render_field_text'; } // Construct the key. $key = Starter_Plugin()->token . '-' . $args['section'] . '[' . $args['id'] . ']'; $method_output = $this->{$method}($key, $args); if (!is_wp_error($method_output)) { $html .= $method_output; } // Output the description, if the current field allows it. if (isset($args['type']) && !in_array($args['type'], (array) apply_filters('starter-plugin-no-description-fields', array('checkbox')))) { if (isset($args['description'])) { $description = '<p class="description">' . wp_kses_post($args['description']) . '</p>' . "\n"; if (in_array($args['type'], (array) apply_filters('starter-plugin-new-line-description-fields', array('textarea', 'select')))) { $description = wpautop($description); } $html .= $description; } } echo $html; }
/** * Validate the settings. * @access public * @since 1.0.0 * @param array $input Inputted data. * @return array Validated data. */ public function validate_settings($input) { return Starter_Plugin()->settings->validate_settings($input); }
/** * Save meta box fields. * * @access public * @since 1.0.0 * @param int $post_id * @return void */ public function meta_box_save($post_id) { global $post, $messages; // Verify if (get_post_type() != $this->post_type || !wp_verify_nonce($_POST['starter-plugin_' . $this->post_type . '_noonce'], plugin_basename(dirname(Starter_Plugin()->plugin_path)))) { return $post_id; } if (isset($_POST['post_type']) && 'page' == esc_attr($_POST['post_type'])) { if (!current_user_can('edit_page', $post_id)) { return $post_id; } } else { if (!current_user_can('edit_post', $post_id)) { return $post_id; } } $field_data = $this->get_custom_fields_settings(); $fields = array_keys($field_data); foreach ($fields as $f) { ${$f} = strip_tags(trim($_POST[$f])); // Escape the URLs. if ('url' == $field_data[$f]['type']) { ${$f} = esc_url(${$f}); } if (get_post_meta($post_id, '_' . $f) == '') { add_post_meta($post_id, '_' . $f, ${$f}, true); } elseif (${$f} != get_post_meta($post_id, '_' . $f, true)) { update_post_meta($post_id, '_' . $f, ${$f}); } elseif (${$f} == '') { delete_post_meta($post_id, '_' . $f, get_post_meta($post_id, '_' . $f, true)); } } }
/** * Validate the settings. * @access public * @since 1.0.0 * @param array $input Inputted data. * @return array Validated data. */ public function validate_settings($input) { $sections = Starter_Plugin()->settings->get_settings_sections(); $tab = $this->_get_current_tab($sections); return Starter_Plugin()->settings->validate_settings($input, $tab); }