register( new ImageSelectorControl() ); $controls_manager->register( new Select2AjaxControl() ); } /** * Register Elementor Widget. * Just put the widget class reference here. * * @param \Elementor\Widgets_Manager $widgets_manager Widgets manager. * * @return void */ public function register_widget( $widgets_manager = null ) { $widgets = apply_filters( 'advanced_news_ticker_elemetor_widgets', [ NewsTicker::class, ] ); if ( ! $widgets_manager ) { $widgets_manager = Plugin::instance()->widgets_manager; } foreach ( $widgets as $class ) { if ( ! class_exists( $class ) ) { continue; } $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(' . esc_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', $editor_style ); } /** * Register Elementor category * * @param \Elementor\Elements_Manager $elements_manager 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 ); } /** * Verify that the current request comes from a user allowed to build content. * * @return void */ protected function verify_editor_request() { check_ajax_referer( self::SELECT2_NONCE ); if ( ! current_user_can( 'edit_posts' ) ) { wp_send_json_error( [ 'message' => esc_html__( 'You are not allowed to do this.', 'advanced-news-ticker' ) ], 403 ); } } /** * Search published posts by title. * * @param string $post_type Post type slug, or 'any'. * @param int $limit Result limit. Use -1 for no limit. * @param string $search Search term. * @param int $paged Page number. * * @return array */ public function get_query_data( $post_type = 'any', $limit = 10, $search = '', $paged = 1 ) { global $wpdb; $limit = (int) $limit; $paged = max( 1, (int) $paged ); $data = []; $params = []; $where = ''; if ( 'any' === $post_type ) { $in_search_post_types = get_post_types( [ 'exclude_from_search' => false ] ); if ( empty( $in_search_post_types ) ) { return $data; } $placeholders = implode( ', ', array_fill( 0, count( $in_search_post_types ), '%s' ) ); $where .= " AND {$wpdb->posts}.post_type IN ( {$placeholders} )"; $params = array_merge( $params, array_values( $in_search_post_types ) ); } elseif ( '' !== $post_type ) { $where .= " AND {$wpdb->posts}.post_type = %s"; $params[] = $post_type; } if ( '' !== $search ) { $where .= " AND {$wpdb->posts}.post_title LIKE %s"; $params[] = '%' . $wpdb->esc_like( $search ) . '%'; } $limit_sql = ''; if ( $limit > 0 ) { $limit = min( $limit, 100 ); $limit_sql = ' LIMIT %d, %d'; $params[] = ( $paged - 1 ) * $limit; $params[] = $limit; } $sql = "SELECT post_title, ID FROM {$wpdb->posts} WHERE post_status = 'publish'{$where} ORDER BY post_title ASC{$limit_sql}"; //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $results = $params ? $wpdb->get_results( $wpdb->prepare( $sql, $params ) ) : $wpdb->get_results( $sql ); if ( ! empty( $results ) ) { foreach ( $results as $row ) { $data[ $row->ID ] = $row->post_title . ' [#' . $row->ID . ']'; } } return $data; } /** * Ajax callback for advanced-news-ticker-select2 search. * * @return void */ public function select2_ajax_posts_filter_autocomplete() { $this->verify_editor_request(); $query_per_page = self::MAX_RESULTS; $paged = ! empty( $_POST['page'] ) ? absint( wp_unslash( $_POST['page'] ) ) : 1; $post_type = ! empty( $_POST['post_type'] ) ? sanitize_key( wp_unslash( $_POST['post_type'] ) ) : 'post'; $source_name = ! empty( $_POST['source_name'] ) ? sanitize_key( wp_unslash( $_POST['source_name'] ) ) : 'post_type'; $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' => $query_per_page, ]; if ( 'all' !== $post_type ) { if ( ! taxonomy_exists( $post_type ) ) { wp_send_json( [ 'results' => [], 'pagination' => [ 'more' => false ] ] ); } $args['taxonomy'] = $post_type; } $terms = get_terms( $args ); $post_list = is_wp_error( $terms ) ? [] : wp_list_pluck( $terms, 'name', 'term_id' ); break; case 'user': if ( ! current_user_can( 'list_users' ) ) { wp_send_json( [ 'results' => [], 'pagination' => [ 'more' => false ] ] ); } $users = []; foreach ( get_users( [ 'search' => "*{$search}*", 'number' => $query_per_page ] ) as $user ) { $users[ $user->ID ] = $user->display_name; } $post_list = $users; break; default: $post_list = $this->get_query_data( $this->sanitize_post_type( $post_type ), $query_per_page, $search, $paged ); } $pagination = count( $post_list ) >= $query_per_page; foreach ( $post_list as $key => $item ) { $results[] = [ 'text' => $item, 'id' => $key, ]; } wp_send_json( [ 'results' => $results, 'pagination' => [ 'more' => $pagination ], ] ); } /** * Ajax callback returning labels for already stored select2 values. * * @return void */ public function select2_ajax_get_posts_value_titles() { $this->verify_editor_request(); $raw_ids = isset( $_POST['id'] ) ? wp_unslash( $_POST['id'] ) : []; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $p_ids = array_filter( array_map( 'absint', (array) $raw_ids ) ); $p_ids = array_slice( array_values( $p_ids ), 0, 100 ); if ( ! $p_ids ) { wp_send_json_error( [] ); } $source_name = ! empty( $_POST['source_name'] ) ? sanitize_key( wp_unslash( $_POST['source_name'] ) ) : ''; $post_type = ! empty( $_POST['post_type'] ) ? sanitize_key( wp_unslash( $_POST['post_type'] ) ) : 'post'; $response = []; switch ( $source_name ) { case 'taxonomy': $args = [ 'hide_empty' => false, 'orderby' => 'name', 'order' => 'ASC', 'include' => $p_ids, ]; if ( 'all' !== $post_type ) { if ( ! taxonomy_exists( $post_type ) ) { wp_send_json_error( [] ); } $args['taxonomy'] = $post_type; } $terms = get_terms( $args ); $response = is_wp_error( $terms ) ? [] : wp_list_pluck( $terms, 'name', 'term_id' ); break; case 'user': if ( ! current_user_can( 'list_users' ) ) { wp_send_json_error( [] ); } $users = []; foreach ( get_users( [ 'include' => $p_ids ] ) as $user ) { $users[ $user->ID ] = $user->display_name . '-' . $user->ID; } $response = $users; break; default: $post_info = get_posts( [ 'post_type' => $this->sanitize_post_type( $post_type ), 'post__in' => $p_ids, 'posts_per_page' => count( $p_ids ), 'post_status' => 'publish', 'suppress_filters' => false, ] ); $response = wp_list_pluck( $post_info, 'post_title', 'ID' ); } if ( ! empty( $response ) ) { wp_send_json_success( [ 'results' => $response ] ); } wp_send_json_error( [] ); } /** * Only allow post types the widget itself offers. * * @param string $post_type Post type slug. * * @return string */ protected function sanitize_post_type( $post_type ) { if ( 'any' === $post_type ) { return 'any'; } return array_key_exists( $post_type, Fns::get_post_types() ) ? $post_type : 'post'; } }