'',
'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 '