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 ?>