register( new ImageSelectorControl() ); $controls_manager->register( new Select2AjaxControl() ); } /** * Register Elementor Widget. * Just put the widget class reference here * @return void */ public function register_widget() { $widgets = apply_filters( 'advanced_news_ticker_elemetor_widgets', [ NewsTicker::class, ] ); foreach ( $widgets as $class ) { Plugin::instance()->widgets_manager->register( new $class ); } } /** * Elementor Editor Style * @return void */ public function editor_style() { $icon = Fns::get_assets_url( 'images/icon.svg' ); $editor_style = '.elementor-element .icon .advanced-news-ticker-el-custom{content: url(' . $icon . ');width: 34px;}'; $editor_style .= '.elementor-control.elementor-control-type-heading .elementor-control-title{color:#93003f;font-size: 14px;}'; wp_add_inline_style( 'elementor-editor', esc_textarea( $editor_style ) ); } /** * Register Elementor category * * @param $elements_manager * * @return void */ public function widget_category( $elements_manager ) { $id = ADVANCED_NEWS_TICKER_PREFIX . '-widgets'; $categories[ $id ] = [ 'title' => esc_html__( 'Advanced News Ticker Addons', 'advanced-news-ticker' ), 'icon' => 'fa fa-plug', ]; $get_all_categories = $elements_manager->get_categories(); $categories = array_merge( $categories, $get_all_categories ); $set_categories = function ( $categories ) { $this->categories = $categories; }; $set_categories->call( $elements_manager, $categories ); } /** * Ajax callback for advanced-news-ticker-select2 * * @param $post_type * @param $limit * @param $search * @param $paged * * @return array */ public function get_query_data( $post_type = 'any', $limit = 10, $search = '', $paged = 1 ) { global $wpdb; $where = ''; $data = []; if ( - 1 == $limit ) { $limit = ''; } elseif ( 0 == $limit ) { $limit = 'limit 0,1'; } else { $offset = 0; if ( $paged ) { $offset = ( $paged - 1 ) * $limit; } $limit = $wpdb->prepare( ' limit %d, %d', esc_sql( $offset ), esc_sql( $limit ) ); } if ( 'any' === $post_type ) { $in_search_post_types = get_post_types( [ 'exclude_from_search' => false ] ); if ( empty( $in_search_post_types ) ) { $where .= ' AND 1=0 '; } else { $where .= " AND {$wpdb->posts}.post_type IN ('" . join( "', '", array_map( 'esc_sql', $in_search_post_types ) ) . "')"; } } elseif ( ! empty( $post_type ) ) { $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", esc_sql( $post_type ) ); } if ( ! empty( $search ) ) { $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_title LIKE %s", '%' . esc_sql( $search ) . '%' ); } $query = "select post_title,ID from $wpdb->posts where post_status = 'publish' {$where} {$limit}"; $results = $wpdb->get_results( $query ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( ! empty( $results ) ) { foreach ( $results as $row ) { $data[ $row->ID ] = $row->post_title . ' [#' . $row->ID . ']'; } } return $data; } /** * Ajax callback for advanced-news-ticker-select2 * * @return void */ public function select2_ajax_posts_filter_autocomplete() { check_ajax_referer( 'advanced-news-ticker-select2-nonce' ); $query_per_page = 15; $post_type = 'post'; $source_name = 'post_type'; $paged = ! empty( $_POST['page'] ) ? sanitize_text_field( wp_unslash( $_POST['page'] ) ) : 1; if ( ! empty( $_POST['post_type'] ) ) { $post_type = sanitize_text_field( sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) ); } if ( ! empty( $_POST['source_name'] ) ) { $source_name = sanitize_text_field( wp_unslash( $_POST['source_name'] ) ); } $search = ! empty( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : ''; $results = $post_list = []; switch ( $source_name ) { case 'taxonomy': $args = [ 'hide_empty' => false, 'orderby' => 'name', 'order' => 'ASC', 'search' => $search, 'number' => '5', ]; if ( $post_type !== 'all' ) { $args['taxonomy'] = $post_type; } $post_list = wp_list_pluck( get_terms( $args ), 'name', 'term_id' ); break; case 'user': $users = []; foreach ( get_users( [ 'search' => "*{$search}*" ] ) as $user ) { $user_id = $user->ID; $user_name = $user->display_name; $users[ $user_id ] = $user_name; } $post_list = $users; break; default: $post_list = $this->get_query_data( $post_type, $query_per_page, $search, $paged ); } $pagination = true; if ( count( $post_list ) < $query_per_page ) { $pagination = false; } if ( ! empty( $post_list ) ) { foreach ( $post_list as $key => $item ) { $results[] = [ 'text' => $item, 'id' => $key, ]; } } wp_send_json( [ 'results' => $results, 'pagination' => [ 'more' => $pagination ], ] ); } /** * Ajax callback for advanced-news-ticker-select2 * * @return void */ public function select2_ajax_get_posts_value_titles() { check_ajax_referer( 'advanced-news-ticker-select2-nonce' ); $pIds = ! empty( $_POST['id'] ) ? array_map( 'intval', wp_unslash( $_POST['id'] ) ) : null; if ( ! $pIds ) { wp_send_json_error( [] ); } $source_name = ! empty( $_POST['source_name'] ) ? sanitize_text_field( wp_unslash( $_POST['source_name'] ) ) : ''; switch ( $source_name ) { case 'taxonomy': $args = [ 'hide_empty' => false, 'orderby' => 'name', 'order' => 'ASC', 'include' => implode( ',', $pIds ), ]; if ( ! empty( $_POST['post_type'] ) && $_POST['post_type'] !== 'all' ) { $args['taxonomy'] = sanitize_text_field( wp_unslash( $_POST['post_type'] ) ); } $response = wp_list_pluck( get_terms( $args ), 'name', 'term_id' ); break; case 'user': $users = []; foreach ( get_users( [ 'include' => $pIds ] ) as $user ) { $user_id = $user->ID; $user_name = $user->display_name . '-' . $user->ID; $users[ $user_id ] = $user_name; } $response = $users; break; default: $post_info = get_posts( [ 'post_type' => ! empty( $_POST['post_type'] ) ? sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) : 'post', 'include' => implode( ',', $pIds ), ] ); $response = wp_list_pluck( $post_info, 'post_title', 'ID' ); } if ( ! empty( $response ) ) { wp_send_json_success( [ 'results' => $response ] ); } else { wp_send_json_error( [] ); } } }