onditions/get-ajax-screen/' . $source, $screen, $args, $this ); break; } if ( $screen ) { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_ajax_handler' ) ); } return $screen; } /** * [get_ajax_stack description] * @return [type] [description] */ public function get_ajax_stack() { return $this->_ajax_stack; } /** * Enqueue JS with ajax conditions * @return [type] [description] */ public function enqueue_ajax_handler() { global $current_screen; $stack = $this->get_ajax_stack(); if ( empty( $stack ) || ! isset( $stack[ $current_screen->id ] ) ) { return; } wp_enqueue_script( 'jet-engine-mb-ajax-conditions', jet_engine()->plugin_url( 'includes/components/meta-boxes/assets/js/ajax-conditions.js' ), array( 'jquery' ), jet_engine()->get_version(), true ); wp_localize_script( 'jet-engine-mb-ajax-conditions', 'JetEnginMBAjaxConditionsData', $stack[ $current_screen->id ] ); wp_localize_script( 'jet-engine-mb-ajax-conditions', 'JetEnginMBAjaxConditionsSettings', array( 'nonce' => wp_create_nonce( 'jet-engine/meta-boxes/conditions' ), ) ); wp_localize_script( 'jet-engine-mb-ajax-conditions', 'JetEnginMBAjaxConditionsHandlers', array( 'default' => null ) ); foreach ( $stack[ $current_screen->id ] as $stack_item ) { $condition_type = $this->get_conditions( $stack_item['condition_type'] ); if ( $condition_type ) { wp_add_inline_script( 'jet-engine-mb-ajax-conditions', $condition_type->get_js_handler(), 'before' ); } } } /** * Ajax callback to check given meta box conditions */ public function ajax_check_conditions() { $nonce = ! empty( $_REQUEST['nonce'] ) ? wp_unslash( $_REQUEST['nonce'] ) : false; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized // There is no senesetive data returned by this callback so there is no need to additonal verfifcation except nonce if ( ! $nonce || ! wp_verify_nonce( $nonce, 'jet-engine/meta-boxes/conditions' ) ) { wp_send_json_error( 'Invalid request' ); } $conditions = ! empty( $_REQUEST['conditions'] ) ? wp_unslash( $_REQUEST['conditions'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitized via structure validation and never used for direct output $request = $_REQUEST; $result = array(); unset( $request['conditions'] ); unset( $request['action'] ); unset( $request['nonce'] ); foreach ( $conditions as $condition ) { $condition_key = ! empty( $condition['condition_type'] ) ? $condition['condition_type'] : false; if ( ! $condition_key ) { continue; } $condition_type = $this->get_conditions( $condition_key ); if ( ! $condition_type ) { continue; } if ( $condition_type->check_condition( array( 'settings' => $condition, 'request' => $request ) ) ) { $result[] = array( 'id' => $condition['meta_box'], 'display' => 'block', ); } else { $result[] = array( 'id' => $condition['meta_box'], 'display' => 'none', ); } } wp_send_json_success( $result ); } /** * Check conditions * * @param [type] $args [description] * @return [type] [description] */ public function check_conditions( $id, $args ) { // Meta box conditions affects only renererd meta box itself so is is nothing to do on front-end on rest API requests if ( ! is_admin() ) { return false; } $active_conditions = ! empty( $args['active_conditions'] ) ? $args['active_conditions'] : array(); foreach ( $active_conditions as $condition_key ) { $condition_type = $this->get_conditions( $condition_key ); if ( ! $condition_type ) { continue; } if ( $condition_type->is_ajax() ) { $this->add_to_ajax_stack( $this->get_screen_name( $args ), array_merge( array( 'condition_type' => $condition_key, 'meta_box' => $id, ), $condition_type->get_ajax_data_from_args( $args ) ) ); } elseif ( ! $condition_type->check_condition( array( 'settings' => $args ) ) ) { return false; } } return true; } }