post_title ) > 50 ) ? mb_substr( $post->post_title, 0, 49 ) . '...' : $post->post_title; $return[] = array( 'id' => $post->ID, 'label' => $title, ); } } echo json_encode( $return ); } else { $error = new \WP_Error( 'error_code', 'ERROR: Wrong credentials.' ); wp_send_json_error( $error ); } wp_die(); } ); \add_action( 'wp_ajax_wsal_settings_get_posts', function () { if ( is_user_logged_in() && current_user_can( 'manage_options' ) ) { // we will pass post IDs and titles to this array. $return = array(); $args = array( 's' => isset( $_GET['q'] ) ? \sanitize_text_field( \wp_unslash( $_GET['q'] ) ) : '', // the search query. 'post_status' => 'publish', // if you don't want drafts to be returned. 'ignore_sticky_posts' => 1, 'posts_per_page' => 50, // how much to show at once. ); // you can use WP_Query, query_posts() or get_posts() here - it doesn't matter. $search_results = new \WP_Query( $args ); if ( $search_results->have_posts() ) { while ( $search_results->have_posts() ) { $search_results->the_post(); // shorten the title a little. $title = ( mb_strlen( $search_results->post->post_title ) > 50 ) ? mb_substr( $search_results->post->post_title, 0, 49 ) . '...' : $search_results->post->post_title; if ( isset( $_GET['type'] ) && 'input' === $_GET['type'] ) { $return[] = array( 'id' => $search_results->post->ID, 'label' => $title, ); } else { $return[] = array( $search_results->post->ID, $title ); // array( Post ID, Post Title ). } } } echo json_encode( $return ); } else { $error = new \WP_Error( 'error_code', 'ERROR: Wrong credentials.' ); wp_send_json_error( $error ); } wp_die(); } ); \add_action( 'wp_ajax_wsal_settings_get_users', function () { if ( is_user_logged_in() && current_user_can( 'manage_options' ) ) { $result = array(); $query_params = array(); if ( isset( $_GET['q'] ) && ! is_null( $_GET['q'] ) ) { $query_params['search'] = '*' . \sanitize_text_field( \wp_unslash( $_GET['q'] ) ) . '*'; $query_params['search_columns'] = array( 'user_login', 'user_email' ); } if ( WP_Helper::is_multisite() ) { $query_params['blog_id'] = 0; } $users = \get_users( $query_params ); if ( MainWP_Addon::check_mainwp_plugin_active() ) { $mainwp_users = MainWP_Helper::find_users_by( $query_params['search_columns'], array( str_replace( '*', '%', $query_params['search'] ) ) ); $users = array_merge( $users, $mainwp_users ); } if ( empty( $users ) ) { return $result; } else { foreach ( $users as $user ) { $return[] = array( 'id' => $user->ID, 'label' => $user->user_login . ' (' . $user->user_email . ')', ); } } echo json_encode( $return ); } else { $error = new \WP_Error( 'error_code', 'ERROR: Wrong credentials.' ); wp_send_json_error( $error ); } wp_die(); } ); \add_action( 'wp_ajax_wsal_settings_get_roles', function () { if ( is_user_logged_in() && current_user_can( 'manage_options' ) ) { $search = ''; if ( isset( $_GET['q'] ) && ! is_null( $_GET['q'] ) ) { $search = \sanitize_text_field( \wp_unslash( $_GET['q'] ) ); } $role_names = WP_Helper::get_translated_roles(); if ( MainWP_Addon::check_mainwp_plugin_active() ) { $mainwp_roles = MainWP_Helper::get_collected_roles(); $role_names = array_merge( $role_names, $mainwp_roles ); } $return = array(); if ( empty( $role_names ) ) { return $return; } else { asort( $role_names ); foreach ( $role_names as $slug => $label ) { if ( ! empty( $search ) ) { if ( false !== \mb_strpos( $slug, $search ) || false !== \mb_strpos( $label, $search ) ) { $return[] = array( 'id' => $slug, 'label' => $label, ); } } else { $return[] = array( 'id' => $slug, 'label' => $label, );} } } echo json_encode( $return ); } else { $error = new \WP_Error( 'error_code', 'ERROR: Wrong credentials.' ); wp_send_json_error( $error ); } wp_die(); } ); \add_action( 'wp_ajax_wsal_settings_get_ips', function () { if ( is_user_logged_in() && current_user_can( 'manage_options' ) ) { $result = array(); $search = ''; if ( isset( $_GET['q'] ) && ! is_null( $_GET['q'] ) ) { $search = \sanitize_text_field( \wp_unslash( $_GET['q'] ) ); } $ips = Occurrences_Entity::get_ips_logged_search( $search ); if ( empty( $ips ) ) { return $result; } else { foreach ( $ips as $ip ) { $return[] = array( 'id' => $ip, 'label' => $ip, ); } } echo json_encode( $return ); } else { $error = new \WP_Error( 'error_code', 'ERROR: Wrong credentials.' ); wp_send_json_error( $error ); } wp_die(); } ); \add_action( 'wp_ajax_wsal_settings_get_sites', function () { if ( is_user_logged_in() && current_user_can( 'manage_options' ) ) { $result = array(); $search = ''; if ( isset( $_GET['q'] ) && ! is_null( $_GET['q'] ) ) { $search = \sanitize_text_field( \wp_unslash( $_GET['q'] ) ); } if ( WP_Helper::is_multisite() || MainWP_Addon::check_mainwp_plugin_active() ) { $sites = array(); if ( function_exists( 'get_sites' ) && class_exists( 'WP_Site_Query' ) ) { $sites = get_sites(); } if ( MainWP_Addon::check_mainwp_plugin_active() ) { $sites = array_merge( $sites, MainWP_Helper::get_all_sites_array() ); } if ( empty( $sites ) ) { return $result; } else { foreach ( $sites as $site ) { if ( property_exists( $site, 'site_name' ) ) { $blogname = $site->site_name; } else { $blogname = \get_blog_option( $site->blog_id, 'blogname' ); } if ( false !== \mb_strpos( \mb_strtolower( $blogname ), \mb_strtolower( $search ) ) ) { $result[] = array( 'id' => $site->blog_id, 'label' => $blogname, ); } } } echo json_encode( $result ); } } else { $error = new \WP_Error( 'error_code', 'ERROR: Wrong credentials.' ); wp_send_json_error( $error ); } wp_die(); } ); } /** * Builds the element * * @param array $settings - Array with settings. * @param string $option_name - Name of the option. * @param mixed $data - The data to show. * * @return void * * @since 5.0.0 */ public static function create( $settings, $option_name, $data ) { self::$item_id = null; self::$item_id_attr = null; self::$item_id_wrap = null; self::$name_attr = null; self::$placeholder_attr = null; self::$custom_class = null; self::$current_value = null; self::$option_type = null; self::$option_name = null; self::$settings = null; self::$edit_type = null; self::$validate_pattern = null; self::$max_chars = null; self::$min = null; self::$max = null; self::$step = null; self::prepare_data( $settings, $option_name, $data ); if ( empty( self::$option_type ) ) { return; } // Options Without Labels. $with_label = false; switch ( self::$option_type ) { case 'tab-title': self::tab_title(); break; case 'header': self::section_head(); break; case 'message': case 'success': case 'error': self::notice_message(); break; case 'hint': self::hint_message(); break; case 'hidden': self::hidden(); break; case 'html': self::html(); break; default: $with_label = true; break; } // Options With Label. if ( $with_label ) { /** Option Start */ self::option_head(); /** The Option */ switch ( self::$option_type ) { case 'text': self::text(); break; case 'arrayText': self::text_array(); break; case 'number': self::number(); break; case 'radio': self::radio(); break; case 'checkbox': self::checkbox(); break; case 'select-multiple': self::multiple_select(); break; case 'select2-multiple': self::multiple_select2(); break; case 'date': self::date(); break; case 'textarea': self::textarea(); break; case 'color': self::color(); break; case 'posts': self::posts(); break; case 'post': self::post(); break; case 'users': self::users(); break; case 'sites': self::sites(); break; case 'roles': self::roles(); break; case 'ips': self::ips(); break; case 'post_titles': self::post_titles(); break; case 'editor': self::editor(); break; case 'fonts': self::fonts(); break; case 'upload': self::upload(); break; case 'upload-font': self::upload_font(); break; case 'typography': self::typography(); break; case 'background': self::background(); break; case 'select': self::select(); break; case 'visual': self::visual(); break; case 'gallery': self::gallery(); break; case 'icon': self::icon(); break; default: break; } /** Option END */ if ( 'upload' !== self::$option_type ) { self::hint(); } echo ''; } } /** * HTML code * * @since 5.0.0 */ private static function html() { if ( ! empty( self::$settings['content'] ) ) { echo self::$settings['content']; } } /** * Setting Description * * @since 5.0.0 */ private static function hint() { if ( ! empty( self::$settings['hint'] ) ) { ?>
class="wsal-img-path" type="text" value="" >
>
class="wsal-font-path" type="text" value="" >
value="" > class="" value="" > > class="wsal-js-switch " type="checkbox" value="true" >
$option ) { ++$i; $checked = ''; if ( ( ! empty( self::$current_value ) && self::$current_value === $option_key ) || ( empty( self::$current_value ) && 1 === $i ) ) { $checked = 'checked="checked"'; } ?>
type="text" value="" data-palette="#000000, #9b59b6, #3498db, #2ecc71, #f1c40f, #34495e, #e74c3c" style="width:80px;">
> type="hidden" value="" >
'400px', 'media_buttons' => false, ); $settings['textarea_name'] = self::$option_name; self::$current_value = ! empty( self::$settings['kses'] ) ? wp_kses_stripslashes( stripslashes( self::$current_value ) ) : self::$current_value; wp_editor( self::$current_value, self::$item_id, $settings ); } /** * Fonts * * @since 5.0.0 */ private static function fonts() { ?> class="wsal-select-font" type="text" value="">

class="">

class="">

type="hidden" value=""> type="number" value="" >

class="wsal-section-title ">

class="option-item ">
type="hidden" value="">
>
'', 'line_height' => '', 'weight' => '', 'transform' => '', ) ); ?>
'', 'class' => '', ) ); self::$settings = $settings; self::$option_name = $option_name; extract( $settings ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract self::$option_type = ! empty( $type ) ? $type : false; self::$required = ! empty( $required ) ? $required : false; self::$min = ! empty( $min ) ? $min : null; self::$max = ! empty( $max ) ? $max : null; self::$step = ! empty( $step ) ? $step : null; if ( 'text' === self::$option_type ) { self::$edit_type = ! empty( $validate ) ? $validate : false; self::$validate_pattern = ! empty( $pattern ) ? $pattern : false; self::$max_chars = ! empty( $max_chars ) ? $max_chars : false; } // ID. self::$item_id .= ! empty( $prefix ) ? $prefix . '-' : ''; self::$item_id .= ! empty( $id ) ? $id : ''; if ( ! empty( self::$item_id ) && ' ' !== self::$item_id ) { self::$item_id = ( 'arrayText' === $type ) ? self::$item_id . '-' . $key : self::$item_id; self::$item_id_attr = 'id="' . self::$item_id . '"'; self::$item_id_wrap = 'id="' . self::$item_id . '-item"'; } // Class. self::$custom_class = ! empty( $class ) ? ' ' . $class . '-options' : ''; // Name. self::$name_attr = 'name="' . $option_name . '"'; // Placeholder. self::$placeholder_attr = ! empty( $placeholder ) ? 'placeholder="' . $placeholder . '"' : ''; // Get the option stored data. if ( ! \is_null( $data ) ) { self::$current_value = $data; } elseif ( ! empty( $default ) ) { self::$current_value = $default; } } /** * Creates an option and draws it * * @param array $value - The array with option data. * * @return void * * @since 2.0.0 */ public static function build_option( array $value ) { $data = null; if ( empty( $value['id'] ) ) { $value['id'] = ' '; } $settings_name = $value['settings_name']; if ( false !== self::get_current_options() && isset( self::get_current_options()[ $value['id'] ] ) ) { $data = self::get_current_options()[ $value['id'] ]; } self::create( $value, $settings_name . '[' . $value['id'] . ']', $data ); } /** * Shows the save button in the settings * * @return void * * @since 2.0.0 */ public static function save_button() { ?>
$search_term, // Search post title only. 'suppress_filters' => false, 'post_status' => 'publish', 'post_type' => 'any', ); add_filter( 'posts_where', array( __CLASS__, 'search_post_title' ), 10, 2 ); $posts = get_posts( $args ); remove_filter( 'posts_where', array( __CLASS__, 'search_post_title' ), 10 ); if ( empty( $posts ) ) { return $result; } return $posts; } /** * Alters WordPress query to search only by post title. * * @param string $where SQL WHERE statement. * @param WP_Query $wp_query WordPress query object. * * @return string * * @since 5.0.0 */ public static function search_post_title( $where, $wp_query ) { $search_term = $wp_query->get( 'search_post_title' ); if ( $search_term ) { global $wpdb; $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . $wpdb->esc_like( $search_term ) . '%\''; } return $where; } } }