PHP Class Jetpack_Display_Posts_Widget, jetpack

Inheritance: extends WP_Widget
ファイルを表示 Open project: automattic/jetpack Class Usage Examples

Public Properties

Property Type Description
$cron_name The name of the cron that will update widget data.
$service_url Remote service API URL prefix.
$widget_options_key_prefix Widget options key prefix.

Public Methods

Method Description
__construct ( )
activate_cron ( ) Activates widget update cron task.
cron_task ( ) : boolean Main cron code. Updates all instances of the widget.
deactivate_cron ( ) Deactivates widget update cron task.
deactivate_cron_static ( ) Deactivates widget update cron task.
enqueue_scripts ( ) Enqueue CSS and JavaScript.
extract_errors_from_blog_data ( array | WP_Error $blog_data ) : string Scan and extract first error from blog data array.
fetch_blog_data ( string $site, array $original_data = [], boolean $site_data_only = false ) : array Fetch site information and posts list for a site.
fetch_posts_for_site ( integer $site_id ) : array | WP_Error Fetch list of posts from the WordPress public API.
fetch_service_endpoint ( string $endpoint, integer $timeout = 15 ) : array | WP_Error Fetch a remote service endpoint and parse it.
fetch_site_info ( string $site ) : array | WP_Error Fetch site information from the WordPress public API
form ( array $instance ) : string | void Display the widget administration form.
format_posts_for_storage ( object $parsed_data ) : array Format the posts for better storage. Drop all the data that is not used.
get_blog_data ( string $site ) : array | WP_Error Gets blog data from the cache.
get_instances_sites ( ) : array | boolean Get a list of unique sites from all instances of the widget.
get_site_hash ( string $site ) : string Expiring transients have a name length maximum of 45 characters, so this function returns an abbreviated MD5 hash to use instead of the full URI.
parse_posts_response ( object | WP_Error $service_response ) : array | WP_Error Parse external API response from the posts list request and handle errors if any occur.
parse_service_response ( array $service_response ) : array | WP_Error Parse data from service response.
parse_site_info_response ( array | WP_Error $service_response ) : array | WP_Error Parse external API response from the site info call and handle errors if they occur.
should_cron_be_running ( ) : boolean Checks if the update cron should be running and returns appropriate result.
update ( $new_instance, $old_instance )
update_instance ( string $site ) Update a widget instance.
widget ( array $args, array $instance ) Set up the widget display on the front end.
wp_add_option ( string $option_name, mixed $option_value ) : mixed This is just to make method mocks in the unit tests easier.
wp_get_option ( string $param ) : mixed This is just to make method mocks in the unit tests easier.
wp_update_option ( string $option_name, mixed $option_value ) : mixed This is just to make method mocks in the unit tests easier.
wp_wp_remote_get ( string $url, array $args = [] ) : array | WP_Error This is just to make method mocks in the unit tests easier.

Method Details

__construct() public method

public __construct ( )

activate_cron() public static method

Activates widget update cron task.
public static activate_cron ( )

cron_task() public method

Main cron code. Updates all instances of the widget.
public cron_task ( ) : boolean
return boolean

deactivate_cron() public method

This is a wrapper over the static method as it provides some syntactic sugar.
public deactivate_cron ( )

deactivate_cron_static() public static method

Deactivates widget update cron task.
public static deactivate_cron_static ( )

enqueue_scripts() public method

Enqueue CSS and JavaScript.
Since: 4.0.0
public enqueue_scripts ( )

extract_errors_from_blog_data() public method

Scan and extract first error from blog data array.
public extract_errors_from_blog_data ( array | WP_Error $blog_data ) : string
$blog_data array | WP_Error Blog data to scan for errors.
return string First error message found

fetch_blog_data() public method

Fetch site information and posts list for a site.
public fetch_blog_data ( string $site, array $original_data = [], boolean $site_data_only = false ) : array
$site string Site to fetch the data for.
$original_data array Optional original data to updated.
$site_data_only boolean Fetch only site information, skip posts list.
return array Updated or new data.

fetch_posts_for_site() public method

Fetch list of posts from the WordPress public API.
public fetch_posts_for_site ( integer $site_id ) : array | WP_Error
$site_id integer The site to fetch the posts for.
return array | WP_Error

fetch_service_endpoint() public method

Timeout is set to 15 seconds right now, because sometimes the WordPress API takes more than 5 seconds to fully respond. Caching is used here so we can avoid re-downloading the same endpoint in a single request.
public fetch_service_endpoint ( string $endpoint, integer $timeout = 15 ) : array | WP_Error
$endpoint string Parametrized endpoint to call.
$timeout integer How much time to wait for the API to respond before failing.
return array | WP_Error

fetch_site_info() public method

Fetch site information from the WordPress public API
public fetch_site_info ( string $site ) : array | WP_Error
$site string URL of the site to fetch the information for.
return array | WP_Error

form() public method

Display the widget administration form.
public form ( array $instance ) : string | void
$instance array Widget instance configuration.
return string | void

format_posts_for_storage() public method

Format the posts for better storage. Drop all the data that is not used.
public format_posts_for_storage ( object $parsed_data ) : array
$parsed_data object Array of posts returned by the APIs.
return array Formatted posts or an empty array if no posts were found.

get_blog_data() public method

Gets blog data from the cache.
public get_blog_data ( string $site ) : array | WP_Error
$site string
return array | WP_Error

get_instances_sites() public method

Get a list of unique sites from all instances of the widget.
public get_instances_sites ( ) : array | boolean
return array | boolean

get_site_hash() public method

Expiring transients have a name length maximum of 45 characters, so this function returns an abbreviated MD5 hash to use instead of the full URI.
public get_site_hash ( string $site ) : string
$site string Site to get the hash for.
return string

parse_posts_response() public method

Parse external API response from the posts list request and handle errors if any occur.
public parse_posts_response ( object | WP_Error $service_response ) : array | WP_Error
$service_response object | WP_Error The raw response to be parsed.
return array | WP_Error

parse_service_response() public method

Do basic error handling for general service and data errors
public parse_service_response ( array $service_response ) : array | WP_Error
$service_response array Response from the service.
return array | WP_Error

parse_site_info_response() public method

Parse external API response from the site info call and handle errors if they occur.
public parse_site_info_response ( array | WP_Error $service_response ) : array | WP_Error
$service_response array | WP_Error The raw response to be parsed.
return array | WP_Error

should_cron_be_running() public method

Checks if the update cron should be running and returns appropriate result.
public should_cron_be_running ( ) : boolean
return boolean If the cron should be running or not.

update() public method

public update ( $new_instance, $old_instance )

update_instance() public method

Update a widget instance.
public update_instance ( string $site )
$site string The site to fetch the latest data for.

widget() public method

Set up the widget display on the front end.
public widget ( array $args, array $instance )
$args array
$instance array

wp_add_option() public method

This is just to make method mocks in the unit tests easier.
public wp_add_option ( string $option_name, mixed $option_value ) : mixed
$option_name string Option name to be added
$option_value mixed Option value
return mixed

wp_get_option() public method

This is just to make method mocks in the unit tests easier.
public wp_get_option ( string $param ) : mixed
$param string Option key to get
return mixed

wp_update_option() public method

This is just to make method mocks in the unit tests easier.
public wp_update_option ( string $option_name, mixed $option_value ) : mixed
$option_name string Option name to be updated
$option_value mixed Option value
return mixed

wp_wp_remote_get() public method

This is just to make method mocks in the unit tests easier.
public wp_wp_remote_get ( string $url, array $args = [] ) : array | WP_Error
$url string The URL to fetch
$args array Optional. Request arguments.
return array | WP_Error

Property Details

$cron_name public_oe static_oe property

The name of the cron that will update widget data.
public static $cron_name

$service_url public_oe property

Remote service API URL prefix.
public $service_url

$widget_options_key_prefix public_oe property

Widget options key prefix.
public $widget_options_key_prefix