'', 'return_limit' => 5, 'display_direction' => 'vertical', 'published_within' => 0, 'sort' => 'score', 'boost' => 'views', 'personalize_results' => false, 'img_src' => 'parsely_thumb', 'display_author' => false, ); /** * Constructor. * * @param Parsely $parsely Instance of Parsely class. */ public function __construct( Parsely $parsely ) { parent::__construct( 'Parsely_Recommended_Widget', __( 'Parse.ly Recommended Widget', 'wp-parsely' ), array( 'classname' => 'Recommended_Widget parsely-recommended-widget-hidden', 'description' => __( 'Display a list of post recommendations, personalized for a visitor or the current post.', 'wp-parsely' ), ) ); $this->parsely = $parsely; } /** * Gets the URL for the Recommendations API (GET /related). * * @see https://www.parse.ly/help/api/recommendations#get-related * * @internal While this is a public method now, this should be moved to a new class. * * @since 2.5.0 * * @param string $site_id Publisher Site ID. * @param int|null $published_within Publication filter start date; see https://www.parse.ly/help/api/time for * formatting details. No restriction by default. * @param string|null $sort What to sort the results by. There are currently 2 valid options: `score`, * which will sort articles by overall relevance and `pub_date` which will sort * results by their publication date. The default is `score`. * @param string|null $boost Available for sort=score only. Sub-sort value to re-rank relevant posts that * received high e.g. views; default is undefined. * @param int $return_limit Number of records to retrieve; defaults to "10". * @return string API URL. */ private function get_api_url( string $site_id, ?int $published_within, ?string $sort, ?string $boost, int $return_limit ): string { $related_api_endpoint = Parsely::PUBLIC_API_BASE_URL . '/related'; $query_args = array( 'apikey' => $site_id, 'sort' => $sort, 'limit' => $return_limit, ); if ( 'score' === $sort && 'no-boost' !== $boost ) { $query_args['boost'] = $boost; } if ( null !== $published_within && 0 !== $published_within ) { $query_args['pub_date_start'] = $published_within . 'd'; } return add_query_arg( $query_args, $related_api_endpoint ); } /** * This is the widget function. * * @param array $args Widget Arguments. * @param array $widget_settings Values saved to the db. */ public function widget( $args, $widget_settings ): void /* @phpstan-ignore-line */ { if ( ! $this->site_id_and_secret_are_populated() ) { return; } $instance = $this->get_widget_settings( $widget_settings ); $removed_title_esc = remove_filter( 'widget_title', 'esc_html' ); /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ $title = apply_filters( 'widget_title', $instance['title'] ); if ( $removed_title_esc ) { add_filter( 'widget_title', 'esc_html' ); } $title_html = $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; echo wp_kses_post( $title_html ); // Set up the variables. $api_url = $this->get_api_url( $this->parsely->get_site_id(), (int) $instance['published_within'], // @phpstan-ignore-line $instance['sort'], $instance['boost'], (int) $instance['return_limit'] // @phpstan-ignore-line ); ?> $current_settings Values saved to the db. */ public function form( $current_settings ): string { if ( ! $this->site_id_and_secret_are_populated() ) { $settings_page_url = add_query_arg( 'page', 'parsely', get_admin_url() . 'options-general.php' ); $message = sprintf( /* translators: %s: Plugin settings page URL */ __( 'The Parse.ly Site ID and Parse.ly API Secret fields need to be populated on the Parse.ly settings page for this widget to work.', 'wp-parsely' ), esc_url( $settings_page_url ) ); echo '

', wp_kses_post( $message ), '

'; return ''; } $instance = $this->get_widget_settings( $current_settings ); // editable fields: title. $title = $instance['title']; $return_limit = $instance['return_limit']; $display_direction = $instance['display_direction']; $published_within = $instance['published_within']; $sort = $instance['sort']; $boost = $instance['boost']; $personalize_results = $instance['personalize_results']; $img_src = $instance['img_src']; $display_author = $instance['display_author']; $instance['return_limit'] = $return_limit; $instance['display_direction'] = $display_direction; $instance['published_within'] = $published_within; $instance['sort'] = $sort; $instance['boost'] = $boost; $instance['personalize_results'] = $personalize_results; $instance['img_src'] = $img_src; $instance['display_author'] = $display_author; $boost_params = $this->get_boost_params(); ?>


value="horizontal" />
value="vertical" />




/>
/>

Boost parameters values and labels. */ private function get_boost_params(): array { return array( 'no-boost' => __( 'No boost', 'wp-parsely' ), 'views' => __( 'Page views', 'wp-parsely' ), 'mobile_views' => __( 'Page views on mobile devices', 'wp-parsely' ), 'tablet_views' => __( 'Page views on tablet devices', 'wp-parsely' ), 'desktop_views' => __( 'Page views on desktop devices', 'wp-parsely' ), 'visitors' => __( 'Unique page visitors, total', 'wp-parsely' ), 'visitors_new' => __( 'New visitors', 'wp-parsely' ), 'visitors_returning' => __( 'Returning visitors', 'wp-parsely' ), 'engaged_minutes' => __( 'Total engagement time in minutes', 'wp-parsely' ), 'avg_engaged' => __( 'Engaged minutes spent by total visitors', 'wp-parsely' ), 'avg_engaged_new' => __( 'Average engaged minutes spent by new visitors', 'wp-parsely' ), 'avg_engaged_returning' => __( 'Average engaged minutes spent by returning visitors', 'wp-parsely' ), 'social_interactions' => __( 'Total social interactions', 'wp-parsely' ), 'fb_interactions' => __( 'Count of Facebook shares, likes, and comments', 'wp-parsely' ), 'tw_interactions' => __( 'Count of Twitter tweets and retweets', 'wp-parsely' ), 'pi_interactions' => __( 'Count of Pinterest pins', 'wp-parsely' ), 'social_referrals' => __( 'Page views where the referrer was any social network', 'wp-parsely' ), 'fb_referrals' => __( 'Page views where the referrer was facebook.com', 'wp-parsely' ), 'tw_referrals' => __( 'Page views where the referrer was twitter.com', 'wp-parsely' ), 'pi_referrals' => __( 'Page views where the referrer was pinterest.com', 'wp-parsely' ), ); } /** * Checks if both the Site ID and API secret settings are populated with * non-empty values. * * @since 2.5.0 * * @return bool True if Site ID and API Secret settings are set. * False otherwise. */ private function site_id_and_secret_are_populated(): bool { return $this->parsely->site_id_is_set() && $this->parsely->api_secret_is_set(); } /** * Returns all widget settings by assigning defaults if a setting isn't present * * @since 3.7.0 * * @param array $settings Widget Options. * * @return Widget_Settings */ public function get_widget_settings( array $settings ) { /** * Variable. * * @var Widget_Settings */ $widget_settings = $settings; return array_merge( self::$default_widget_settings, $widget_settings ); } }