|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Methods used for adding and saving meta data for WP Featherlight. |
| 4 | + * |
| 5 | + * @package WPFeatherlight\Admin |
| 6 | + * @copyright Copyright (c) 2016, WP Site Care |
| 7 | + * @license GPL-2.0+ |
| 8 | + * @since 0.1.0 |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * The main featherlight admin meta box and related methods. |
| 13 | + * |
| 14 | + * @since 0.1.0 |
| 15 | + */ |
| 16 | +class WP_Featherlight_Admin_Meta { |
| 17 | + /** |
| 18 | + * Name for the nonce field |
| 19 | + * |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + private $nonce_name = 'wp_featherlight_metabox_nonce'; |
| 23 | + |
| 24 | + /** |
| 25 | + * Name for the nonce action |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + private $nonce_action = 'save_wp_featherlight_metabox'; |
| 30 | + |
| 31 | + /** |
| 32 | + * Determine if the request to save data should be allowed to proceed. |
| 33 | + * |
| 34 | + * @since 1.0.0 |
| 35 | + * @access protected |
| 36 | + * @param int $post_id Post ID. |
| 37 | + * @return bool Whether or not this is a valid request to save our data. |
| 38 | + */ |
| 39 | + protected function validate_request( $post_id ) { |
| 40 | + if ( 'POST' !== $_SERVER['REQUEST_METHOD'] ) { // Input var okay. |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + if ( ! isset( $_POST[ $this->nonce_name ] ) ) { // Input var okay. |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + if ( ! wp_verify_nonce( sanitize_key( $_POST[ $this->nonce_name ] ), $this->nonce_action ) ) { // Input var okay. |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + // @link http://make.marketpress.com/multilingualpress/2014/10/how-to-disable-broken-save_post-callbacks/ |
| 69 | + if ( is_multisite() && ms_is_switched() ) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + return wp_unslash( $_POST ); // Input var okay. |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Output the content of our metabox. |
| 78 | + * |
| 79 | + * @since 1.0.0 |
| 80 | + * @access public |
| 81 | + * |
| 82 | + * @param WP_Post $post Post object. |
| 83 | + * @return void |
| 84 | + */ |
| 85 | + public function meta_box_view( WP_Post $post ) { |
| 86 | + $type = get_post_type_object( $post->post_type ); |
| 87 | + |
| 88 | + if ( ! is_object( $type ) ) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + if ( current_user_can( $type->cap->edit_post, $post->ID ) && $type->public ) { |
| 93 | + $disable = get_post_meta( $post->ID, 'wp_featherlight_disable', true ); |
| 94 | + $checked = empty( $disable ) ? '' : $disable; |
| 95 | + |
| 96 | + require_once wp_featherlight()->get_dir() . 'admin/views/meta-box.php'; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Callback function for saving our meta box data. |
| 102 | + * |
| 103 | + * @since 1.0.0 |
| 104 | + * @access public |
| 105 | + * @param int $post_id Post ID. |
| 106 | + * @return bool Whether or not data has been saved. |
| 107 | + */ |
| 108 | + public function save_meta_boxes( $post_id ) { |
| 109 | + if ( ! $valid_request = $this->validate_request( $post_id ) ) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + $value = isset( $valid_request['wp_featherlight_disable'] ) ? 'yes' : ''; |
| 114 | + |
| 115 | + return (bool) update_post_meta( $post_id, 'wp_featherlight_disable', $value ); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Add a metabox to control featherlight display options. |
| 120 | + * |
| 121 | + * @since 0.1.0 |
| 122 | + * @access public |
| 123 | + * @param string $post_type the current post type. |
| 124 | + * @return void |
| 125 | + */ |
| 126 | + public function add_meta_boxes( $post_type ) { |
| 127 | + _deprecated_function( __METHOD__, '1.0.0' ); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Output the content of our metabox. |
| 132 | + * |
| 133 | + * @deprecated 1.0.0 |
| 134 | + * @access public |
| 135 | + * |
| 136 | + * @param WP_Post $post Post object. |
| 137 | + * @return void |
| 138 | + */ |
| 139 | + public function options_callback( WP_Post $post ) { |
| 140 | + _deprecated_function( __METHOD__, '1.0.0' ); |
| 141 | + |
| 142 | + $this-> meta_box_view( $post ); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Get the class running! |
| 147 | + * |
| 148 | + * @since 0.1.0 |
| 149 | + * @deprecated 1.0.0 |
| 150 | + * @access public |
| 151 | + * @return void |
| 152 | + */ |
| 153 | + public function run() { |
| 154 | + _deprecated_function( __METHOD__, '1.0.0' ); |
| 155 | + } |
| 156 | +} |
0 commit comments