'', 'return_limit' => 5, 'display_direction' => 'vertical', 'published_within' => 0, 'sort' => 'score', '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). * * @since 2.5.0 * * @see https://docs.parse.ly/content-recommendations/ * * @internal While this is a public method now, this should be moved to a new class. * * @param string $site_id Publisher Site ID. * @param int|null $published_within Publication filter start date; see https://docs.parse.ly/api-date-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 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, int $return_limit ): string { $related_api_endpoint = $this->parsely->get_content_api()->get_endpoint( '/related' ); $query_args = array( 'apikey' => $site_id, 'sort' => $sort, 'limit' => $return_limit, ); if ( null !== $published_within && 0 !== $published_within ) { $query_args['pub_date_start'] = $published_within . 'd'; } return $related_api_endpoint->get_endpoint_url( $query_args ); } /** * 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'], (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-settings', get_admin_url() . 'admin.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']; $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['personalize_results'] = $personalize_results; $instance['img_src'] = $img_src; $instance['display_author'] = $display_author; ?>


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



/>
/>

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 ); } }