privacy
5 years ago
promo
6 years ago
yit-ajax.php
7 years ago
yit-assets.php
5 years ago
yit-cpt-unlimited.php
6 years ago
yit-debug.php
7 years ago
yit-icons.php
7 years ago
yit-metabox.php
6 years ago
yit-plugin-common.php
8 years ago
yit-plugin-gradients.php
6 years ago
yit-plugin-licence.php
5 years ago
yit-plugin-panel-wc.php
5 years ago
yit-plugin-panel.php
5 years ago
yit-plugin-subpanel.php
6 years ago
yit-pointers.php
6 years ago
yit-theme-licence.php
7 years ago
yit-upgrade.php
7 years ago
yit-video.php
7 years ago
yith-dashboard.php
7 years ago
yith-gutenberg.php
7 years ago
yith-system-status.php
5 years ago
yit-metabox.php
642 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This file belongs to the YIT Framework. |
| 4 | * This source file is subject to the GNU GENERAL PUBLIC LICENSE (GPL 3.0) |
| 5 | * that is bundled with this package in the file LICENSE.txt. |
| 6 | * It is also available through the world-wide-web at this URL: |
| 7 | * http://www.gnu.org/licenses/gpl-3.0.txt |
| 8 | */ |
| 9 | |
| 10 | |
| 11 | if ( !defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } // Exit if accessed directly |
| 14 | |
| 15 | if ( !class_exists( 'YIT_Metabox' ) ) { |
| 16 | /** |
| 17 | * YIT Metabox |
| 18 | * the metabox can be created using this code |
| 19 | * <code> |
| 20 | * $args1 = array( |
| 21 | * 'label' => __( 'Metabox Label', 'yith-plugin-fw' ), |
| 22 | * 'pages' => 'page', //or array( 'post-type1', 'post-type2') |
| 23 | * 'context' => 'normal', //('normal', 'advanced', or 'side') |
| 24 | * 'priority' => 'default', |
| 25 | * 'tabs' => array( |
| 26 | * 'settings' => array( //tab |
| 27 | * 'label' => __( 'Settings', 'yith-plugin-fw' ), |
| 28 | * 'fields' => array( |
| 29 | * 'meta_checkbox' => array( |
| 30 | * 'label' => __( 'Show title', 'yith-plugin-fw' ), |
| 31 | * 'desc' => __( 'Choose whether to show title of the page or not.', 'yith-plugin-fw' ), |
| 32 | * 'type' => 'checkbox', |
| 33 | * 'private' => false, |
| 34 | * 'std' => '1'), |
| 35 | * ), |
| 36 | * ), |
| 37 | * ); |
| 38 | * $metabox1 = YIT_Metabox( 'yit-metabox-id' ); |
| 39 | * $metabox1->init( $args ); |
| 40 | * </code> |
| 41 | * |
| 42 | * @class YIT_Metaboxes |
| 43 | * @package YITH |
| 44 | * @since 1.0.0 |
| 45 | * @author Emanuela Castorina <emanuela.castorina@yithemes.com> |
| 46 | */ |
| 47 | class YIT_Metabox { |
| 48 | |
| 49 | /** |
| 50 | * @var string the id of metabox |
| 51 | * @since 1.0 |
| 52 | */ |
| 53 | |
| 54 | public $id; |
| 55 | |
| 56 | /** |
| 57 | * @var array An array where are saved all metabox settings options |
| 58 | * @since 1.0 |
| 59 | */ |
| 60 | private $options = array(); |
| 61 | |
| 62 | /** |
| 63 | * @var array An array where are saved all tabs of metabox |
| 64 | * @since 1.0 |
| 65 | */ |
| 66 | private $tabs = array(); |
| 67 | |
| 68 | /** |
| 69 | * @var object The single instance of the class |
| 70 | * @since 1.0 |
| 71 | */ |
| 72 | protected static $_instance = array(); |
| 73 | |
| 74 | /** |
| 75 | * Main Instance |
| 76 | * |
| 77 | * @static |
| 78 | * @param $id |
| 79 | * @return object Main instance |
| 80 | * @since 1.0 |
| 81 | * @author Antonino Scarfi' <antonino.scarfi@yithemes.com> |
| 82 | */ |
| 83 | public static function instance( $id ) { |
| 84 | if ( !isset( self::$_instance[ $id ] ) ) { |
| 85 | self::$_instance[ $id ] = new self( $id ); |
| 86 | } |
| 87 | |
| 88 | return self::$_instance[ $id ]; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Constructor |
| 93 | * |
| 94 | * @param string $id |
| 95 | * @return \YIT_Metabox |
| 96 | * @since 1.0 |
| 97 | * @author Emanuela Castorina <emanuela.castorina@yithemes.it> |
| 98 | */ |
| 99 | function __construct( $id = '' ) { |
| 100 | $this->id = $id; |
| 101 | |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * Init |
| 107 | * set options and tabs, add actions to register metabox, scripts and save data |
| 108 | * |
| 109 | * @param array $options |
| 110 | * @return void |
| 111 | * @since 1.0 |
| 112 | * @author Emanuela Castorina <emanuela.castorina@yithemes.it> |
| 113 | */ |
| 114 | public function init( $options = array() ) { |
| 115 | |
| 116 | $this->set_options( $options ); |
| 117 | $this->set_tabs(); |
| 118 | |
| 119 | add_action( 'add_meta_boxes', array( $this, 'register_metabox' ), 99 ); |
| 120 | add_action( 'save_post', array( $this, 'save_postdata' ), 10, 1 ); |
| 121 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 15 ); |
| 122 | |
| 123 | add_filter( 'yit_icons_screen_ids', array( $this, 'add_screen_ids_for_icons' ) ); |
| 124 | |
| 125 | add_action( 'wp_ajax_yith_plugin_fw_save_toggle_element_metabox', array( $this, 'save_toggle_element' ) ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Add Screen ids to include icons |
| 130 | * |
| 131 | * @param $screen_ids |
| 132 | * @return array |
| 133 | */ |
| 134 | public function add_screen_ids_for_icons( $screen_ids ) { |
| 135 | return array_unique( array_merge( $screen_ids, (array) $this->options[ 'pages' ] ) ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Enqueue script and styles in admin side |
| 140 | * Add style and scripts to administrator |
| 141 | * |
| 142 | * @return void |
| 143 | * @since 1.0 |
| 144 | * @author Emanuela Castorina <emanuela.castorina@yithemes.it> |
| 145 | * @author Leanza Francesco <leanzafrancesco@gmail.com> |
| 146 | */ |
| 147 | public function enqueue() { |
| 148 | $enqueue = function_exists( 'get_current_screen' ) && get_current_screen() && in_array( get_current_screen()->id, (array) $this->options[ 'pages' ] ); |
| 149 | $enqueue = apply_filters( 'yith_plugin_fw_metabox_enqueue_styles_and_scripts', $enqueue, $this ); |
| 150 | |
| 151 | // load scripts and styles only where the metabox is displayed |
| 152 | if ( $enqueue ) { |
| 153 | wp_enqueue_media(); |
| 154 | |
| 155 | wp_enqueue_style( 'woocommerce_admin_styles' ); |
| 156 | |
| 157 | wp_enqueue_style( 'yith-plugin-fw-fields' ); |
| 158 | wp_enqueue_style( 'wp-color-picker' ); |
| 159 | wp_enqueue_style( 'yit-plugin-metaboxes' ); |
| 160 | wp_enqueue_style( 'yit-jquery-ui-style' ); |
| 161 | |
| 162 | wp_enqueue_script( 'yit-metabox' ); |
| 163 | |
| 164 | wp_enqueue_script( 'yith-plugin-fw-fields' ); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Set Options |
| 170 | * Set the variable options |
| 171 | * |
| 172 | * @param array $options |
| 173 | * @return void |
| 174 | * @since 1.0 |
| 175 | * @author Emanuela Castorina <emanuela.castorina@yithemes.it> |
| 176 | */ |
| 177 | public function set_options( $options = array() ) { |
| 178 | $this->options = $options; |
| 179 | |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Set Tabs |
| 184 | * Set the variable tabs |
| 185 | * |
| 186 | * @return void |
| 187 | * @internal param array $tabs |
| 188 | * @since 1.0 |
| 189 | * @author Emanuela Castorina <emanuela.castorina@yithemes.it> |
| 190 | */ |
| 191 | public function set_tabs() { |
| 192 | if ( !isset( $this->options[ 'tabs' ] ) ) { |
| 193 | return; |
| 194 | } |
| 195 | $this->tabs = $this->options[ 'tabs' ]; |
| 196 | if ( isset( $this->tabs[ 'settings' ][ 'fields' ] ) ) { |
| 197 | $this->tabs[ 'settings' ][ 'fields' ] = array_filter( $this->tabs[ 'settings' ][ 'fields' ] ); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | |
| 202 | /** |
| 203 | * Add Tab |
| 204 | * Add a tab inside the metabox |
| 205 | * |
| 206 | * @param array $tab the new tab to add to the metabox |
| 207 | * @param string $where tell where insert the tab if after or before a $refer |
| 208 | * @param null $refer an existent tab inside metabox |
| 209 | * @return void |
| 210 | * @internal param array $tabs |
| 211 | * @since 1.0 |
| 212 | * @author Emanuela Castorina <emanuela.castorina@yithemes.it> |
| 213 | */ |
| 214 | public function add_tab( $tab, $where = 'after', $refer = null ) { |
| 215 | if ( !is_null( $refer ) ) { |
| 216 | $ref_pos = array_search( $refer, array_keys( $this->tabs ) ); |
| 217 | if ( $ref_pos !== false ) { |
| 218 | if ( $where == 'after' ) { |
| 219 | $this->tabs = array_slice( $this->tabs, 0, $ref_pos + 1, true ) + |
| 220 | $tab + |
| 221 | array_slice( $this->tabs, $ref_pos + 1, count( $this->tabs ) - 1, true ); |
| 222 | } else { |
| 223 | $this->tabs = array_slice( $this->tabs, 0, $ref_pos, true ) + |
| 224 | $tab + |
| 225 | array_slice( $this->tabs, $ref_pos, count( $this->tabs ), true ); |
| 226 | } |
| 227 | } |
| 228 | } else { |
| 229 | $this->tabs = array_merge( $tab, $this->tabs ); |
| 230 | } |
| 231 | |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Remove Tab |
| 236 | * Remove a tab from the tabs of metabox |
| 237 | * |
| 238 | * @param $id_tab |
| 239 | * @return void |
| 240 | * @internal param array $tabs |
| 241 | * @since 1.0 |
| 242 | * @author Emanuela Castorina <emanuela.castorina@yithemes.it> |
| 243 | */ |
| 244 | public function remove_tab( $id_tab ) { |
| 245 | if ( isset( $this->tabs[ $id_tab ] ) ) { |
| 246 | unset ( $this->tabs[ $id_tab ] ); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | |
| 251 | /** |
| 252 | * Add Field |
| 253 | * Add a field inside a tab of metabox |
| 254 | * |
| 255 | * @param string $tab_id the id of the tabs where add the field |
| 256 | * @param array $args the field to add |
| 257 | * @param string $where tell where insert the field if after or before a $refer |
| 258 | * @param null $refer an existent field inside tab |
| 259 | * @return void |
| 260 | * @internal param array $tabs |
| 261 | * @since 1.0 |
| 262 | * @author Emanuela Castorina <emanuela.castorina@yithemes.it> |
| 263 | */ |
| 264 | public function add_field( $tab_id, $args, $where = 'after', $refer = null ) { |
| 265 | if ( isset( $this->tabs[ $tab_id ] ) ) { |
| 266 | |
| 267 | $cf = $this->tabs[ $tab_id ][ 'fields' ]; |
| 268 | if ( !is_null( $refer ) ) { |
| 269 | $ref_pos = array_search( $refer, array_keys( $cf ) ); |
| 270 | if ( $ref_pos !== false ) { |
| 271 | if ( $where == 'after' ) { |
| 272 | $this->tabs[ $tab_id ][ 'fields' ] = array_slice( $cf, 0, $ref_pos + 1, true ) + |
| 273 | $args + |
| 274 | array_slice( $cf, $ref_pos, count( $cf ) - 1, true ); |
| 275 | |
| 276 | } elseif ( $where == 'before' ) { |
| 277 | $this->tabs[ $tab_id ][ 'fields' ] = array_slice( $cf, 0, $ref_pos, true ) + |
| 278 | $args + |
| 279 | array_slice( $cf, $ref_pos, count( $cf ), true ); |
| 280 | |
| 281 | } |
| 282 | } |
| 283 | } else { |
| 284 | if ( $where == 'first' ) { |
| 285 | $this->tabs[ $tab_id ][ 'fields' ] = $args + $cf; |
| 286 | |
| 287 | } else { |
| 288 | $this->tabs[ $tab_id ][ 'fields' ] = array_merge( $this->tabs[ $tab_id ][ 'fields' ], $args ); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | } |
| 293 | |
| 294 | |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Remove Field |
| 299 | * Remove a field from the metabox, search inside the tabs and remove it if exists |
| 300 | * |
| 301 | * @param $id_field |
| 302 | * @return void |
| 303 | * @since 1.0 |
| 304 | * @author Emanuela Castorina <emanuela.castorina@yithemes.it> |
| 305 | */ |
| 306 | public function remove_field( $id_field ) { |
| 307 | foreach ( $this->tabs as $tab_name => $tab ) { |
| 308 | if ( isset( $tab[ 'fields' ][ $id_field ] ) ) { |
| 309 | unset ( $this->tabs[ $tab_name ][ 'fields' ][ $id_field ] ); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Reorder tabs |
| 316 | * Order the tabs and fields and set id and name to each field |
| 317 | * |
| 318 | * @return void |
| 319 | * @internal param $id_field |
| 320 | * @since 1.0 |
| 321 | * @author Emanuela Castorina <emanuela.castorina@yithemes.it> |
| 322 | */ |
| 323 | public function reorder_tabs() { |
| 324 | foreach ( $this->tabs as $tab_name => $tab ) { |
| 325 | foreach ( $tab[ 'fields' ] as $id_field => $field ) { |
| 326 | $this->tabs[ $tab_name ][ 'fields' ][ $id_field ][ 'private' ] = ( isset( $field[ 'private' ] ) ) ? $field[ 'private' ] : true; |
| 327 | if ( empty( $this->tabs[ $tab_name ][ 'fields' ][ $id_field ][ 'id' ] ) ) |
| 328 | $this->tabs[ $tab_name ][ 'fields' ][ $id_field ][ 'id' ] = $this->get_option_metabox_id( $id_field, $this->tabs[ $tab_name ][ 'fields' ][ $id_field ][ 'private' ] ); |
| 329 | if ( empty( $this->tabs[ $tab_name ][ 'fields' ][ $id_field ][ 'name' ] ) ) |
| 330 | $this->tabs[ $tab_name ][ 'fields' ][ $id_field ][ 'name' ] = $this->get_option_metabox_name( $this->tabs[ $tab_name ][ 'fields' ][ $id_field ][ 'id' ] ); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | } |
| 335 | |
| 336 | |
| 337 | /** |
| 338 | * Get Option Metabox ID |
| 339 | * return the id of the field |
| 340 | * |
| 341 | * @param string $id_field |
| 342 | * @param bool $private if private add an _befor the id |
| 343 | * @return string |
| 344 | * @since 1.0 |
| 345 | * @author Emanuela Castorina <emanuela.castorina@yithemes.it> |
| 346 | */ |
| 347 | public function get_option_metabox_id( $id_field, $private = true ) { |
| 348 | if ( $private ) { |
| 349 | return '_' . $id_field; |
| 350 | } else { |
| 351 | return $id_field; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Get Option Metabox Name |
| 357 | * return the name of the field, this name will be used as attribute name of the input field |
| 358 | * |
| 359 | * @param string $id_field |
| 360 | * @param bool $private if private add an _befor the id |
| 361 | * @return string |
| 362 | * @since 1.0 |
| 363 | * @author Emanuela Castorina <emanuela.castorina@yithemes.it> |
| 364 | */ |
| 365 | public function get_option_metabox_name( $id_field, $private = true ) { |
| 366 | $db_name = apply_filters( 'yit_metaboxes_option_main_name', 'yit_metaboxes' ); |
| 367 | $return = $db_name . '['; |
| 368 | |
| 369 | if ( !strpos( $id_field, '[' ) ) { |
| 370 | return $return . $id_field . ']'; |
| 371 | } |
| 372 | $return .= substr( $id_field, 0, strpos( $id_field, '[' ) ); |
| 373 | $return .= ']'; |
| 374 | $return .= substr( $id_field, strpos( $id_field, '[' ) ); |
| 375 | |
| 376 | return $return; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Register the metabox |
| 381 | * call the wp function add_metabox to add the metabox |
| 382 | * |
| 383 | * @return void |
| 384 | * @since 1.0 |
| 385 | * @author Emanuela Castorina <emanuela.castorina@yithemes.it> |
| 386 | */ |
| 387 | public function register_metabox( $post_type ) { |
| 388 | |
| 389 | if ( in_array( $post_type, (array) $this->options[ 'pages' ] ) ) { |
| 390 | add_meta_box( $this->id, $this->options[ 'label' ], array( $this, 'show' ), $post_type, $this->options[ 'context' ], $this->options[ 'priority' ] ); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Show metabox |
| 396 | * show the html of metabox |
| 397 | * |
| 398 | * @return void |
| 399 | * @since 1.0 |
| 400 | * @author Emanuela Castorina <emanuela.castorina@yithemes.it> |
| 401 | */ |
| 402 | public function show() { |
| 403 | $this->reorder_tabs(); |
| 404 | |
| 405 | $args = array( |
| 406 | 'tabs' => $this->tabs, |
| 407 | 'class' => isset( $this->options[ 'class' ] ) ? $this->options[ 'class' ] : '', |
| 408 | ); |
| 409 | |
| 410 | yit_plugin_get_template( YIT_CORE_PLUGIN_PATH, 'metaboxes/tab.php', $args ); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Save Post Data |
| 415 | * Save the post data in the database when save the post |
| 416 | * |
| 417 | * @param $post_id |
| 418 | * @return int |
| 419 | * @since 1.0 |
| 420 | * @author Emanuela Castorina <emanuela.castorina@yithemes.it> |
| 421 | */ |
| 422 | public function save_postdata( $post_id ) { |
| 423 | if ( !isset( $_POST[ 'yit_metaboxes_nonce' ] ) || !wp_verify_nonce( $_POST[ 'yit_metaboxes_nonce' ], 'metaboxes-fields-nonce' ) ) { |
| 424 | return $post_id; |
| 425 | } |
| 426 | |
| 427 | $allow_ajax = isset( $_REQUEST[ 'yith_metabox_allow_ajax_saving' ] ) && $this->id === $_REQUEST[ 'yith_metabox_allow_ajax_saving' ]; |
| 428 | if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX && !$allow_ajax ) ) { |
| 429 | return $post_id; |
| 430 | } |
| 431 | |
| 432 | if ( isset( $_POST[ 'post_type' ] ) ) { |
| 433 | $post_type = $_POST[ 'post_type' ]; |
| 434 | } else { |
| 435 | return $post_id; |
| 436 | } |
| 437 | |
| 438 | if ( 'page' === $post_type ) { |
| 439 | if ( !current_user_can( 'edit_page', $post_id ) ) { |
| 440 | return $post_id; |
| 441 | } |
| 442 | } else { |
| 443 | if ( !current_user_can( 'edit_post', $post_id ) ) { |
| 444 | return $post_id; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | if ( !in_array( $post_type, (array) $this->options[ 'pages' ] ) ) { |
| 449 | return $post_id; |
| 450 | } |
| 451 | |
| 452 | if ( isset( $_POST[ 'yit_metaboxes' ] ) ) { |
| 453 | $yit_metabox_data = $_POST[ 'yit_metaboxes' ]; |
| 454 | |
| 455 | if ( is_array( $yit_metabox_data ) ) { |
| 456 | foreach ( $yit_metabox_data as $field_name => $field_value ) { |
| 457 | if ( !add_post_meta( $post_id, $field_name, $field_value, true ) ) { |
| 458 | update_post_meta( $post_id, $field_name, $field_value ); |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | $this->sanitize_and_save_fields( $post_id ); |
| 465 | |
| 466 | return $post_id; |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * @param $post_id |
| 471 | * @since 3.2.1 |
| 472 | * @deprecated since 3.4.8 |
| 473 | */ |
| 474 | public function sanitize_fields( $post_id ) { |
| 475 | $this->sanitize_and_save_fields( $post_id ); |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Sanitize and save fields of the Metabox. |
| 480 | * |
| 481 | * @return void |
| 482 | * @since 3.4.8 |
| 483 | */ |
| 484 | public function sanitize_and_save_fields( $post_id ) { |
| 485 | $this->reorder_tabs(); |
| 486 | $tabs_to_sanitize = $this->tabs; |
| 487 | $allow_ajax = isset( $_REQUEST[ 'yith_metabox_allow_ajax_saving' ] ) && $this->id === $_REQUEST[ 'yith_metabox_allow_ajax_saving' ]; |
| 488 | $ajax_partial_saving_tab = isset( $_REQUEST[ 'yith_metabox_allow_ajax_partial_saving_tab' ] ) ? $_REQUEST[ 'yith_metabox_allow_ajax_partial_saving_tab' ] : false; |
| 489 | |
| 490 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX && !$allow_ajax ) { |
| 491 | return; |
| 492 | } elseif ( $ajax_partial_saving_tab ) { |
| 493 | if ( array_key_exists( $ajax_partial_saving_tab, $tabs_to_sanitize ) ) { |
| 494 | $tabs_to_sanitize = array( $ajax_partial_saving_tab => $tabs_to_sanitize[ $ajax_partial_saving_tab ] ); |
| 495 | } else { |
| 496 | return; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | foreach ( $tabs_to_sanitize as $tab ) { |
| 501 | foreach ( $tab[ 'fields' ] as $field ) { |
| 502 | $this->sanitize_and_save_field( $field, $post_id ); |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Sanitize and save a single field |
| 509 | * |
| 510 | * @param array $field |
| 511 | * @param int $post_id |
| 512 | * @since 3.4.8 |
| 513 | */ |
| 514 | public function sanitize_and_save_field( $field, $post_id ) { |
| 515 | if ( in_array( $field[ 'type' ], array( 'title' ) ) ) { |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | if ( isset( $_POST[ 'yit_metaboxes' ][ $field[ 'id' ] ] ) ) { |
| 520 | if ( in_array( $field[ 'type' ], array( 'onoff', 'checkbox' ) ) ) { |
| 521 | update_post_meta( $post_id, $field[ 'id' ], '1' ); |
| 522 | } elseif ( in_array( $field[ 'type' ], array( 'toggle-element' ) ) ) { |
| 523 | if ( isset( $field[ 'elements' ] ) && $field[ 'elements' ] ) { |
| 524 | $elements_value = $_POST[ 'yit_metaboxes' ][ $field[ 'id' ] ]; |
| 525 | if ( $elements_value ) { |
| 526 | if ( isset( $elements_value[ 'box_id' ] ) ) { |
| 527 | unset( $elements_value[ 'box_id' ] ); |
| 528 | } |
| 529 | |
| 530 | foreach ( $field[ 'elements' ] as $element ) { |
| 531 | foreach ( $elements_value as $key => $element_value ) { |
| 532 | if ( isset( $field[ 'onoff_field' ] ) ) { |
| 533 | $elements_value[ $key ][ $field[ 'onoff_field' ][ 'id' ] ] = !isset( $element_value[ $field[ 'onoff_field' ][ 'id' ] ] ) ? 0 : $element_value[ $field[ 'onoff_field' ][ 'id' ] ]; |
| 534 | } |
| 535 | if ( in_array( $element[ 'type' ], array( 'onoff', 'checkbox' ) ) ) { |
| 536 | $elements_value[ $key ][ $element[ 'id' ] ] = !isset( $element_value[ $element[ 'id' ] ] ) ? 0 : 1; |
| 537 | } |
| 538 | |
| 539 | if ( !empty( $element[ 'yith-sanitize-callback' ] ) && is_callable( $element[ 'yith-sanitize-callback' ] ) ) { |
| 540 | $elements_value[ $key ][ $element[ 'id' ] ] = call_user_func( $element[ 'yith-sanitize-callback' ], $elements_value[ $key ][ $element[ 'id' ] ] ); |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | update_post_meta( $post_id, $field[ 'id' ], maybe_serialize( $elements_value ) ); |
| 547 | } |
| 548 | } else { |
| 549 | $value = $_POST[ 'yit_metaboxes' ][ $field[ 'id' ] ]; |
| 550 | if ( !empty( $field[ 'yith-sanitize-callback' ] ) && is_callable( $field[ 'yith-sanitize-callback' ] ) ) { |
| 551 | $value = call_user_func( $field[ 'yith-sanitize-callback' ], $value ); |
| 552 | } |
| 553 | add_post_meta( $post_id, $field[ 'id' ], $value, true ) || update_post_meta( $post_id, $field[ 'id' ], $value ); |
| 554 | } |
| 555 | } elseif ( in_array( $field[ 'type' ], array( 'onoff', 'checkbox' ) ) ) { |
| 556 | update_post_meta( $post_id, $field[ 'id' ], '0' ); |
| 557 | } elseif ( in_array( $field[ 'type' ], array( 'checkbox-array' ) ) ) { |
| 558 | update_post_meta( $post_id, $field[ 'id' ], array() ); |
| 559 | } else { |
| 560 | delete_post_meta( $post_id, $field[ 'id' ] ); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Remove Fields |
| 566 | * Remove a fields list from the metabox, search inside the tabs and remove it if exists |
| 567 | * |
| 568 | * @param $id_fields |
| 569 | * @return void |
| 570 | * @since 2.0.0 |
| 571 | * @author Andrea Grillo <andrea.grillo@yithemes.com> |
| 572 | */ |
| 573 | public function remove_fields( $id_fields ) { |
| 574 | foreach ( $id_fields as $k => $field ) { |
| 575 | $this->remove_field( $field ); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * Save the element toggle via Ajax. |
| 581 | * |
| 582 | * @return void |
| 583 | * @since 3.2.1 |
| 584 | * @author Emanuela Castorina |
| 585 | */ |
| 586 | public function save_toggle_element() { |
| 587 | if ( !isset( $_REQUEST[ 'post_ID' ] ) ) { |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | if ( !isset( $_REQUEST[ 'yit_metaboxes_nonce' ] ) || !wp_verify_nonce( $_REQUEST[ 'yit_metaboxes_nonce' ], 'metaboxes-fields-nonce' ) ) { |
| 592 | return; |
| 593 | } |
| 594 | $post_id = $_REQUEST[ 'post_ID' ]; |
| 595 | |
| 596 | if ( isset( $_REQUEST[ 'yit_metaboxes' ], $_REQUEST[ 'toggle_id' ], $_REQUEST[ 'metabox_tab' ], $_REQUEST[ 'yit_metaboxes' ][ $_REQUEST[ 'toggle_id' ] ] ) ) { |
| 597 | $yit_metabox_data = $_REQUEST[ 'yit_metaboxes' ]; |
| 598 | $metabox_tab = $_REQUEST[ 'metabox_tab' ]; |
| 599 | $field_id = $_REQUEST[ 'toggle_id' ]; |
| 600 | if ( strpos( $field_id, '_' ) === 0 ) { |
| 601 | $field_id = substr( $field_id, 1 ); |
| 602 | } |
| 603 | |
| 604 | if ( is_array( $yit_metabox_data ) ) { |
| 605 | $this->reorder_tabs(); |
| 606 | $tabs = $this->tabs; |
| 607 | |
| 608 | if ( isset( $tabs[ $metabox_tab ], $tabs[ $metabox_tab ][ 'fields' ] ) && isset( $tabs[ $metabox_tab ][ 'fields' ][ $field_id ] ) ) { |
| 609 | $field = $tabs[ $metabox_tab ][ 'fields' ][ $field_id ]; |
| 610 | if ( $field ) { |
| 611 | $this->sanitize_and_save_field( $field, $post_id ); |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | } elseif ( isset( $_REQUEST[ 'toggle_id' ] ) ) { |
| 616 | delete_post_meta( $post_id, $_REQUEST[ 'toggle_id' ] ); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | if ( !function_exists( 'YIT_Metabox' ) ) { |
| 623 | |
| 624 | /** |
| 625 | * Main instance of plugin |
| 626 | * |
| 627 | * @param $id |
| 628 | * @return \YIT_Metabox |
| 629 | * @since 1.0 |
| 630 | * @author Emanuela Castorina <emanuela.castorina@yithemes.it> |
| 631 | */ |
| 632 | |
| 633 | |
| 634 | function YIT_Metabox( $id ) { |
| 635 | return YIT_Metabox::instance( $id ); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 |