load_presets(); $this->new_preset_action(); $this->remove_preset_action(); $this->search_preset_action(); add_action( 'wp_ajax_xts_save_preset_action', array( $this, 'save_preset_action' ) ); add_action( 'wp_ajax_xts_get_entity_ids_action', array( $this, 'get_entity_ids_action' ) ); } /** * Load presets from the database. */ public function load_presets() { $presets = get_option( 'xts-options-presets' ); if ( empty( $presets ) ) { $presets = array(); } self::$presets = $presets; } /** * Create new preset action. */ public function search_preset_action() { if ( empty( $_GET['search_preset'] ) ) { // phpcs:ignore return; } $presets = self::get_all(); $search = sanitize_text_field( wp_unslash( $_GET['search_preset'] ) ); // phpcs:ignore foreach ( $presets as $id => $preset ) { if ( ! str_contains( strtolower( $preset['name'] ), strtolower( $search ) ) ) { unset( self::$presets[ $id ] ); } } } /** * Create new preset action. */ public function new_preset_action() { if ( ! isset( $_POST['xts_presets_add_new'] ) || ! isset( $_POST['_wp_http_referer'] ) ) { return; } if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['xts_presets_add_new'] ) ), 'xts_presets' ) ) { return; } $id = $this->add_preset( 'New preset' ); wp_safe_redirect( add_query_arg( array( 'preset_id' => $id, 'action' => 'add_new', ), remove_query_arg( [ 'preset_id', 'action', 'preset_page', 'search_preset' ], sanitize_text_field( wp_unslash( $_POST['_wp_http_referer'] ) ) ) ) ); } /** * Remove preset action. * * @since 1.0.0 */ public function remove_preset_action() { if ( ! isset( $_POST['preset_id'] ) || ! isset( $_POST['xts_presets_remove'] ) || ! isset( $_POST['_wp_http_referer'] ) ) { return; } if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['xts_presets_remove'] ) ), 'xts_presets' ) ) { return; } $this->remove_preset( (int) sanitize_text_field( wp_unslash( $_POST['preset_id'] ) ) ); $presets_pages = self::get_paginated_presets(); $preset_page = isset( $_GET['preset_page'] ) ? sanitize_text_field( wp_unslash( $_GET['preset_page'] ) ) : 0; if ( ! isset( $presets_pages[ $preset_page ] ) ) { --$preset_page; } wp_safe_redirect( add_query_arg( array( 'action' => 'removed', 'preset_page' => $preset_page, ), remove_query_arg( [ 'preset_id', 'action' ], sanitize_text_field( wp_unslash( $_POST['_wp_http_referer'] ) ) ) ) ); } /** * AJAX action for saving preset conditions. */ public function save_preset_action() { check_ajax_referer( 'xts_presets_nonce', 'security' ); $preset_id = isset( $_POST['preset_id'] ) ? (int) sanitize_text_field( wp_unslash( $_POST['preset_id'] ) ) : false; $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : false; $priority = isset( $_POST['priority'] ) ? sanitize_text_field( wp_unslash( $_POST['priority'] ) ) : false; $condition = array( 'relation' => 'OR', 'rules' => array(), ); if ( $preset_id && isset( $_POST['data'] ) && is_array( $_POST['data'] ) ) { foreach ( $_POST['data'] as $rule ) { // phpcs:ignore $condition['rules'][] = wp_parse_args( $rule, array( 'type' => '', 'comparison' => '=', 'post_type' => '', 'taxonomy' => '', 'custom' => '', 'value_id' => '', 'user_role' => '', ) ); } } $this->update_preset_conditions( $preset_id, $condition ); $this->update_preset_priority( $preset_id, $priority ); $this->update_preset_name( $preset_id, $name ); wp_send_json_success( array( 'message' => esc_html__( 'Preset edited successfully.', 'woodmart' ), ) ); } /** * Update preset conditions. * * @param integer $id Preset id. * @param array $condition Conditions array. */ public function update_preset_conditions( $id, $condition ) { self::$presets[ $id ]['condition'] = $condition; $this->update_presets(); } /** * Update preset priority. * * @param integer $id Preset id. * @param string $priority Priority. */ public function update_preset_priority( $id, $priority ) { self::$presets[ $id ]['priority'] = $priority; $this->update_presets(); } /** * Update preset name. * * @param integer $id Preset id. * @param string $name Name. */ public function update_preset_name( $id, $name ) { self::$presets[ $id ]['name'] = $name; $this->update_presets(); } /** * Create a preset in the database. * * @param integer $name Preset name. * * @return int|string|null */ public function add_preset( $name ) { $all = self::get_all(); ksort( $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. * * @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. */ public function update_presets() { update_option( 'xts-options-presets', self::$presets ); } /** * AJAX action to load entities names. */ public function get_entity_ids_action() { check_ajax_referer( 'xts_presets_nonce', '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': case 'single_posts_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 . ' (ID:' . $term->term_id . ')', ); } } 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 . ' (ID:' . $post->ID . ')', ); } } break; } echo wp_json_encode( array( 'results' => $items, ) ); wp_die(); } /** * Get paginated presets. */ public static function get_paginated_presets() { return array_chunk( self::get_all(), 10, true ); } /** * Enqueue scripts. */ public static function enqueue_scripts() { wp_enqueue_script( 'wd-presets', WOODMART_ASSETS . '/js/presets.js', array(), WOODMART_VERSION, true ); wp_enqueue_script( 'select2', WOODMART_ASSETS . '/js/libs/select2.full.min.js', array(), WOODMART_VERSION, true ); } /** * User interface. */ public static function output_ui() { self::enqueue_scripts(); $presets_pages = self::get_paginated_presets(); $current_page = isset( $_GET['preset_page'] ) ? sanitize_text_field( wp_unslash( $_GET['preset_page'] ) ) : 0; // phpcs:ignore ?>

$preset ) : ?> 1 ) : ?>

'', 'comparison' => '=', 'post_type' => '', 'taxonomy' => '', 'custom' => '', 'value_id' => '', 'user_role' => '', ) ); $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', 'is_rtl' => 'Is RTL', ) ); $user_roles = get_editable_roles(); $title = false; if ( 'post_id' === $rule['type'] && ! empty( $rule['value_id'] ) ) { $title = get_the_title( $rule['value_id'] ); } if ( ( 'term_id' === $rule['type'] || 'single_posts_term_id' === $rule['type'] ) && ! empty( $rule['value_id'] ) ) { $taxonomies = get_taxonomies(); foreach ( $taxonomies as $taxonomy ) { $term_object = get_term_by( 'id', $rule['value_id'], $taxonomy ); if ( $term_object ) { $title = $term_object->name . ' (ID:' . $term_object->term_id . ')'; } } } ?>
'ids' ) ); if ( $terms ) { $condition = in_array( $rule['value_id'], $terms, false ); // phpcs:ignore $is_active = 'equals' === $rule['comparison'] ? $condition : ! $condition; } } break; case 'term_id': $object = get_queried_object(); $term_id = false; if ( is_object( $object ) && property_exists( $object, 'term_id' ) ) { $term_id = get_queried_object()->term_id; $type = get_queried_object()->taxonomy; $ancestors = get_ancestors( $term_id, $type ); if ( in_array( $rule['value_id'], $ancestors, false ) ) { // phpcs:ignore $term_id = $rule['value_id']; } } if ( $term_id ) { $condition = (int) $term_id === (int) $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 = (int) $taxonomy === (int) $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 = (int) woodmart_get_the_ID() === (int) get_option( 'page_for_posts' ); $is_active = 'equals' === $rule['comparison'] ? $condition : ! $condition; break; case 'front': $condition = (int) woodmart_get_the_ID() === (int) 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': if ( woodmart_woocommerce_installed() ) { $is_active = 'equals' === $rule['comparison'] ? is_shop() : ! is_shop(); } break; case 'single_product': if ( woodmart_woocommerce_installed() ) { $is_active = 'equals' === $rule['comparison'] ? is_product() : ! is_product(); } break; case 'cart': if ( woodmart_woocommerce_installed() ) { $is_active = 'equals' === $rule['comparison'] ? is_cart() : ! is_cart(); } break; case 'checkout': if ( woodmart_woocommerce_installed() ) { $is_active = 'equals' === $rule['comparison'] ? is_checkout() : ! is_checkout(); } break; case 'account': if ( woodmart_woocommerce_installed() ) { $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; case 'is_rtl': $is_active = 'equals' === $rule['comparison'] ? is_rtl() : ! is_rtl(); break; } break; case 'user_role': $condition = in_array( $rule['user_role'], woodmart_get_current_user_roles(), true ); $is_active = 'equals' === $rule['comparison'] ? $condition : ! $condition; break; } if ( isset( $_GET['page'] ) && isset( $_GET['preset'] ) && 'xts_theme_settings' === $_GET['page'] ) { // phpcs:ignore $is_active = true; $preset['id'] = $_GET['preset']; // phpcs:ignore } if ( $is_active ) { $priority = isset( $preset['priority'] ) ? $preset['priority'] : ''; $active_presets[ $priority ] = $preset['id']; } } } foreach ( $all as $preset ) { if ( isset( $_GET['opts'] ) && $preset['name'] === $_GET['opts'] ) { // phpcs:ignore $active_presets[] = $preset['id']; } } ksort( $active_presets ); return apply_filters( 'xts_active_options_presets', array_unique( $active_presets ), $all ); } } Presets::get_instance();