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() { ?>