load_presets(); } /** * Load presets from the database. * * @since 1.0.0 */ public function load_presets() { $presets = get_option( 'xts-options-presets' ); if ( ! $presets || empty( $presets ) ) { $presets = array(); } self::$presets = $presets; } /** * AJAX action for saving preset conditions. * * @since 1.0.0 */ public function save_preset_conditions_action() { check_ajax_referer( 'xts-preset-form', 'security' ); $id = isset( $_POST['preset'] ) ? (int) sanitize_text_field( $_POST['preset'] ) : false; // phpcs:ignore $condition = array( 'relation' => 'OR', 'rules' => array(), ); if ( $id && isset( $_POST['data'] ) && is_array( $_POST['data'] ) ) { foreach ( $_POST['data'] as $key => $rule ) { // phpcs:ignore $condition['rules'][] = wp_parse_args( $rule, array( 'type' => '', 'comparison' => '=', 'post_type' => '', 'taxonomy' => '', 'custom' => '', 'value_id' => '', ) ); } } $this->update_preset_conditions( $id, $condition ); $this->ajax_response( array( 'error_msg' => esc_html__( 'Something went wrong during the AJAX request.', 'woodmart' ), 'success_msg' => esc_html__( 'Options preset has been successfully saved.', 'woodmart' ), ) ); wp_die(); } /** * Update preset conditions. * * @since 1.0.0 * * @param integer $id Preset's id. * @param array $condition Conditions array. */ public function update_preset_conditions( $id, $condition ) { self::$presets[ $id ]['condition'] = $condition; $this->update_presets(); } /** * Create preset AJAX action. * * @since 1.0.0 */ public function new_preset_action() { check_ajax_referer( 'xts-preset-form', 'security' ); $name = isset( $_POST['name'] ) ? sanitize_text_field( $_POST['name'] ) : 'New preset'; // phpcs:ignore $id = $this->add_preset( $name ); $this->ajax_response( array( 'id' => $id, ) ); wp_die(); } /** * Remove preset AJAX action. * * @since 1.0.0 */ public function remove_preset_action() { check_ajax_referer( 'xts-preset-form', 'security' ); $id = isset( $_POST['id'] ) ? (int) sanitize_text_field( $_POST['id'] ) : false; // phpcs:ignore if ( ! $id ) { wp_die(); } $this->remove_preset( $id ); $this->ajax_response(); wp_die(); } /** * Create a preset in the database. * * @since 1.0.0 * * @param integer $name Preset name. * * @return int|string|null */ public function add_preset( $name ) { $all = self::get_all(); end( $all ); $last_id = key( $all ); if ( empty( $all ) ) { $last_id = apply_filters( 'xts_presets_start_id', 0 ); } $id = $last_id + 1; $new_preset = array( 'id' => $id, 'name' => $name, 'condition' => array(), ); self::$presets[ $id ] = $new_preset; $this->update_presets(); return $id; } /** * Remove preset from the database. * * @since 1.0.0 * * @param integer $id Remove preset by id. */ public function remove_preset( $id ) { if ( ! isset( self::$presets[ $id ] ) ) { return; } unset( self::$presets[ $id ] ); $this->update_presets(); } /** * Update presets option. * * @since 1.0.0 */ public function update_presets() { update_option( 'xts-options-presets', self::$presets ); } /** * Send AJAX response data. * * @since 1.0.0 * * @param array $data Additional data array. */ public function ajax_response( $data = array() ) { ob_start(); self::output_ui(); $ui = ob_get_clean(); echo wp_json_encode( array_merge( array( 'ui' => $ui, ), $data ) ); } /** * AJAX action to load entities names. * * @since 1.0.0 */ public function get_entity_ids_action() { check_ajax_referer( 'xts-preset-form', 'security' ); $type = isset( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : false; // phpcs:ignore $name = isset( $_POST['name'] ) ? sanitize_text_field( $_POST['name'] ) : false; // phpcs:ignore $items = array(); switch ( $type ) { case 'term_id': $args = array( 'hide_empty' => false, 'fields' => 'all', 'name__like' => $name, ); $terms = get_terms( $args ); if ( count( $terms ) > 0 ) { foreach ( $terms as $term ) { $items[] = array( 'id' => $term->term_id, 'text' => $term->name, ); } } break; case 'post_id': $args = array( 's' => $name, 'post_type' => get_post_types( array( 'public' => true ) ), 'posts_per_page' => 100, ); $posts = get_posts( $args ); if ( count( $posts ) > 0 ) { foreach ( $posts as $post ) { $items[] = array( 'id' => $post->ID, 'text' => $post->post_title, ); } } break; } echo wp_json_encode( array( 'results' => $items, ) ); wp_die(); } /** * Output Presets UI. * * @since 1.0.0 */ public static function output_ui() { ?>

'', 'comparison' => '=', 'post_type' => '', 'taxonomy' => '', 'custom' => '', 'value_id' => '', ) ); $post_types = get_post_types( array( 'public' => true, ) ); $taxonomies = get_taxonomies( array( 'public' => true, ) ); $custom_conditions = apply_filters( 'xts_get_custom_conditions_for_preset', array( 'search' => 'Search results', 'blog' => 'Default "Your Latest Posts" screen', 'front' => 'Front page', 'archives' => 'All archives', 'author' => 'Author archives', 'error404' => '404 error screens', 'shop' => 'Shop page', 'single_product' => 'Single product', 'cart' => 'Cart page', 'checkout' => 'Checkout page', 'account' => 'Account pages', 'is_mobile' => 'Is mobile device', ) ); $title = false; if ( 'post_id' === $rule['type'] && ! empty( $rule['value_id'] ) ) { $title = get_the_title( $rule['value_id'] ); } if ( 'term_id' === $rule['type'] && ! empty( $rule['value_id'] ) ) { $term_object = false; $taxonomies = get_taxonomies(); foreach ( $taxonomies as $tax_type_key => $taxonomy ) { $term_object = get_term_by( 'id', $rule['value_id'], $taxonomy ); if ( ! $term_object ) { break; } } if ( $term_object ) { $title = $term_object->name; } } ?>
term_id : false; if ( $term_id ) { $condition = $term_id == $rule['value_id']; $is_active = 'equals' === $rule['comparison'] ? $condition : ! $condition; } break; case 'taxonomy': $object = get_queried_object(); $taxonomy = is_object( $object ) && property_exists( $object, 'taxonomy' ) ? get_queried_object()->taxonomy : false; if ( $taxonomy ) { $condition = $taxonomy == $rule['taxonomy']; $is_active = 'equals' === $rule['comparison'] ? $condition : ! $condition; } break; case 'custom': switch ( $rule['custom'] ) { case 'search': $is_active = 'equals' === $rule['comparison'] ? is_search() : ! is_search(); break; case 'blog': $condition = woodmart_get_the_ID() == get_option( 'page_for_posts' ); $is_active = 'equals' === $rule['comparison'] ? $condition : ! $condition; break; case 'front': $condition = woodmart_get_the_ID() == get_option( 'page_on_front' ); $is_active = 'equals' === $rule['comparison'] ? $condition : ! $condition; break; case 'archives': $is_active = 'equals' === $rule['comparison'] ? is_archive() : ! is_archive(); break; case 'author': $is_active = 'equals' === $rule['comparison'] ? is_author() : ! is_author(); break; case 'error404': $is_active = 'equals' === $rule['comparison'] ? is_404() : ! is_404(); break; case 'shop': $is_active = 'equals' === $rule['comparison'] ? is_shop() : ! is_shop(); break; case 'single_product': $is_active = 'equals' === $rule['comparison'] ? is_product() : ! is_product(); break; case 'cart': $is_active = 'equals' === $rule['comparison'] ? is_cart() : ! is_cart(); break; case 'checkout': $is_active = 'equals' === $rule['comparison'] ? is_checkout() : ! is_checkout(); break; case 'account': $is_active = 'equals' === $rule['comparison'] ? is_account_page() : ! is_account_page(); break; case 'is_mobile': $is_active = 'equals' === $rule['comparison'] ? wp_is_mobile() : ! wp_is_mobile(); break; } break; } if ( isset( $_GET['page'] ) && isset( $_GET['preset'] ) && 'xtemos_options' === $_GET['page'] ) { // phpcs:ignore $is_active = true; $preset['id'] = $_GET['preset']; // phpcs:ignore } if ( $is_active ) { $active_presets[] = $preset['id']; } } } foreach ( $all as $preset ) { if ( isset( $_GET['opts'] ) && $preset['name'] === $_GET['opts'] ) { // phpcs:ignore array_push( $active_presets, $preset['id'] ); } } return apply_filters( 'xts_active_options_presets', array_unique( $active_presets ), $all ); } } Presets::get_instance();