" . $html;
return $html;
}
/**
* Options to be added to the discussion page (see also admin_settings_init, etc below for Sharing settings page)
*/
public function admin_discussion_likes_settings_init() {
// Add a temporary section, until we can move the setting out of there and with the rest of the email notification settings.
add_settings_section( 'likes-notifications', __( 'Likes Notifications', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_section' ), 'discussion' );
add_settings_field( 'social-notifications', __( 'Email me whenever', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_field' ), 'discussion', 'likes-notifications' );
// Register the setting.
register_setting( 'discussion', 'social_notifications_like', array( $this, 'admin_discussion_likes_settings_validate' ) );
}
/** Add email notification options to WordPress discussion settings */
public function admin_discussion_likes_settings_section() {
// Atypical usage here. We emit jquery to move likes notification checkbox to be with the rest of the email notification settings.
?>
admin_likes_get_option( 'social_notifications_like' );
?>
settings->is_likes_module_enabled() ) {
return;
}
if ( ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ||
( defined( 'APP_REQUEST' ) && APP_REQUEST ) ||
( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) ||
( defined( 'COOKIE_AUTH_REQUEST' ) && COOKIE_AUTH_REQUEST ) ||
( defined( 'JABBER_SERVER' ) && JABBER_SERVER ) ) {
return;
}
if (
class_exists( 'Jetpack_AMP_Support' )
&& Jetpack_AMP_Support::is_amp_request()
) {
return;
}
add_filter( 'the_content', array( $this, 'post_likes' ), 30, 1 );
add_filter( 'the_excerpt', array( $this, 'post_likes' ), 30, 1 );
}
/**
* Load the CSS needed for the wp-admin area.
*/
public function load_admin_css() {
?>
' . __( 'Likes', 'jetpack' ) . '';
$columns['date'] = $date;
return $columns;
}
/**
* Append like button to content.
*
* @param string $content - content of the page.
*/
public function post_likes( $content ) {
global $wp_current_filter;
$post_id = get_the_ID();
if ( ! is_numeric( $post_id ) || ! $this->settings->is_likes_visible() ) {
return $content;
}
// Do not output Likes on requests for ActivityPub requests.
if (
function_exists( '\Activitypub\is_activitypub_request' )
&& \Activitypub\is_activitypub_request()
) {
return $content;
}
// Ensure we don't display like button on post excerpts that are hooked inside the post content
if ( in_array( 'the_excerpt', (array) $wp_current_filter, true ) &&
in_array( 'the_content', (array) $wp_current_filter, true ) ) {
return $content;
}
$blog_id = Jetpack_Options::get_option( 'id' );
$url = home_url();
$url_parts = wp_parse_url( $url );
$domain = $url_parts['host'];
// Make sure to include the `queuehandler` scripts before the iframe otherwise the script won't find the iframe.
if ( ! has_action( 'wp_footer', 'jetpack_likes_master_iframe' ) ) {
add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 );
}
/**
* If the same post appears more then once on a page the page goes crazy
* we need a slightly more unique id / name for the widget wrapper.
*/
$uniqid = uniqid();
$src = sprintf( 'https://widgets.wp.com/likes/?ver=%1$s#blog_id=%2$d&post_id=%3$d&origin=%4$s&obj_id=%2$d-%3$d-%5$s', rawurlencode( JETPACK__VERSION ), $blog_id, $post_id, $domain, $uniqid );
$name = sprintf( 'like-post-frame-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid );
$wrapper = sprintf( 'like-post-wrapper-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid );
$headline = sprintf(
/** This filter is already documented in modules/sharedaddy/sharing-service.php */
apply_filters( 'jetpack_sharing_headline_html', '%s
', esc_html__( 'Like this:', 'jetpack' ), 'likes' ),
esc_html__( 'Like this:', 'jetpack' )
);
$title = esc_html__( 'Like or Reblog', 'jetpack' );
/** This filter is documented in modules/likes/jetpack-likes-master-iframe.php */
$src = apply_filters( 'jetpack_likes_iframe_src', $src );
$html = "';
// Let's make sure that the script is enqueued.
wp_enqueue_script( 'jetpack_likes_queuehandler' );
return $content . $html;
}
}
/**
* Callback to get the value for the jetpack_likes_enabled field.
*
* Warning: this behavior is somewhat complicated!
* When the switch_like_status post_meta is unset, we follow the global setting in Sharing.
* When it is set to 0, we disable likes on the post, regardless of the global setting.
* When it is set to 1, we enable likes on the post, regardless of the global setting.
*
* @param array $post - post data we're checking.
*
* @return bool
*/
function jetpack_post_likes_get_value( array $post ) {
if ( ! isset( $post['id'] ) ) {
return false;
}
$post_likes_switched = get_post_meta( $post['id'], 'switch_like_status', true );
/** This filter is documented in modules/jetpack-likes-settings.php */
$sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
// An empty string: post meta was not set, so go with the global setting.
if ( '' === $post_likes_switched ) {
return $sitewide_likes_enabled;
} elseif ( '0' === $post_likes_switched ) { // User overrode the global setting to disable likes.
return false;
} elseif ( '1' === $post_likes_switched ) { // User overrode the global setting to enable likes.
return true;
}
// No default fallback, let's stay explicit.
}
/**
* Callback to set switch_like_status post_meta when jetpack_likes_enabled is updated.
*
* Warning: this behavior is somewhat complicated!
* When the switch_like_status post_meta is unset, we follow the global setting in Sharing.
* When it is set to 0, we disable likes on the post, regardless of the global setting.
* When it is set to 1, we enable likes on the post, regardless of the global setting.
*
* @param bool $enable_post_likes - checks if post likes are enabled.
* @param object $post_object - object containing post data.
*/
function jetpack_post_likes_update_value( $enable_post_likes, $post_object ) {
/** This filter is documented in modules/jetpack-likes-settings.php */
$sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
$should_switch_status = $enable_post_likes !== $sitewide_likes_enabled;
if ( $should_switch_status ) {
// Set the meta to 0 if the user wants to disable likes, 1 if user wants to enable.
$switch_like_status = ( $enable_post_likes ? 1 : 0 );
return update_post_meta( $post_object->ID, 'switch_like_status', $switch_like_status );
} else {
// Unset the meta otherwise.
return delete_post_meta( $post_object->ID, 'switch_like_status' );
}
}
/**
* Add Likes post_meta to the REST API Post response.
*
* @action rest_api_init
* @uses register_rest_field
* @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/
*/
function jetpack_post_likes_register_rest_field() {
$post_types = get_post_types( array( 'public' => true ) );
foreach ( $post_types as $post_type ) {
register_rest_field(
$post_type,
'jetpack_likes_enabled',
array(
'get_callback' => 'jetpack_post_likes_get_value',
'update_callback' => 'jetpack_post_likes_update_value',
'schema' => array(
'description' => __( 'Are Likes enabled?', 'jetpack' ),
'type' => 'boolean',
),
)
);
/**
* Ensures all public internal post-types support `likes`
* This feature support flag is used by the REST API and Gutenberg.
*/
add_post_type_support( $post_type, 'jetpack-post-likes' );
}
}
// Add Likes post_meta to the REST API Post response.
add_action( 'rest_api_init', 'jetpack_post_likes_register_rest_field' );
// Some CPTs (e.g. Jetpack portfolios and testimonials) get registered with
// restapi_theme_init because they depend on theme support, so let's also hook to that.
add_action( 'restapi_theme_init', 'jetpack_post_likes_register_rest_field', 20 );
Jetpack_Likes::init();