2021-07-23 11:58:50 +02:00
< ? php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2019-11-15 23:26:29 +01:00
/**
* Module Name : Likes
* Module Description : Give visitors an easy way to show they appreciate your content .
* First Introduced : 2.2
* Sort Order : 23
* Requires Connection : Yes
* Auto Activate : No
* Module Tags : Social
* Feature : Engagement
* Additional Search Queries : like , likes , wordpress . com
2021-07-23 11:58:50 +02:00
*
* @ package automattic / jetpack
2019-11-15 23:26:29 +01:00
*/
2022-12-15 17:41:53 +01:00
/**
* NOTE : While the front - end behavior currently varies , try to keep the data
* model here the same as on wpcom to facilitate Simple→Atomic moves and
* possible future work to recombine the front - ends .
*/
2019-11-15 23:26:29 +01:00
2023-04-26 17:39:43 +02:00
// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
2019-11-15 23:26:29 +01:00
use Automattic\Jetpack\Assets ;
2020-09-15 14:30:05 +02:00
Assets :: add_resource_hint (
array (
'//widgets.wp.com' ,
'//s0.wp.com' ,
'//0.gravatar.com' ,
'//1.gravatar.com' ,
'//2.gravatar.com' ,
),
'dns-prefetch'
);
2019-11-15 23:26:29 +01:00
2021-07-23 11:58:50 +02:00
require_once __DIR__ . '/likes/jetpack-likes-master-iframe.php' ;
require_once __DIR__ . '/likes/jetpack-likes-settings.php' ;
2019-11-15 23:26:29 +01:00
2021-07-23 11:58:50 +02:00
/**
* Jetpack Like Class
*/
2019-11-15 23:26:29 +01:00
class Jetpack_Likes {
2021-07-23 11:58:50 +02:00
2023-01-25 20:43:46 +01:00
/**
* Jetpack_Likes_Settings object
*
* @ var Jetpack_Likes_Settings
*/
public $settings ;
2021-07-23 11:58:50 +02:00
/**
* Initialize class
*/
2019-11-15 23:26:29 +01:00
public static function init () {
2021-07-23 11:58:50 +02:00
static $instance = null ;
2019-11-15 23:26:29 +01:00
if ( ! $instance ) {
$instance = new Jetpack_Likes ();
}
return $instance ;
}
2021-07-23 11:58:50 +02:00
/**
* Constructs Likes class
*/
public function __construct () {
2022-12-15 17:41:53 +01:00
$this -> settings = new Jetpack_Likes_Settings ();
2019-11-15 23:26:29 +01:00
// We need to run on wp hook rather than init because we check is_amp_endpoint()
2021-07-23 11:58:50 +02:00
// when bootstrapping hooks.
add_action ( 'wp' , array ( $this , 'action_init' ), 99 );
2019-11-15 23:26:29 +01:00
add_action ( 'admin_init' , array ( $this , 'admin_init' ) );
2022-12-15 17:41:53 +01:00
add_action ( 'jetpack_activate_module_likes' , array ( $this , 'set_social_notifications_like' ) );
add_action ( 'jetpack_deactivate_module_likes' , array ( $this , 'delete_social_notifications_like' ) );
2019-11-15 23:26:29 +01:00
2022-12-15 17:41:53 +01:00
Jetpack :: enable_module_configurable ( __FILE__ );
add_filter ( 'jetpack_module_configuration_url_likes' , array ( $this , 'jetpack_likes_configuration_url' ) );
add_action ( 'admin_print_scripts-settings_page_sharing' , array ( $this , 'load_jp_css' ) );
add_filter ( 'sharing_show_buttons_on_row_start' , array ( $this , 'configuration_target_area' ) );
2019-11-15 23:26:29 +01:00
2022-12-15 17:41:53 +01:00
$active = Jetpack :: get_active_modules ();
2019-11-15 23:26:29 +01:00
2022-12-15 17:41:53 +01:00
if ( ! in_array ( 'sharedaddy' , $active , true ) && ! in_array ( 'publicize' , $active , true ) ) {
// we don't have a sharing page yet.
add_action ( 'admin_menu' , array ( $this -> settings , 'sharing_menu' ) );
}
2019-11-15 23:26:29 +01:00
2022-12-15 17:41:53 +01:00
if ( in_array ( 'publicize' , $active , true ) && ! in_array ( 'sharedaddy' , $active , true ) ) {
// we have a sharing page but not the global options area.
add_action ( 'pre_admin_screen_sharing' , array ( $this -> settings , 'sharing_block' ), 20 );
add_action ( 'pre_admin_screen_sharing' , array ( $this -> settings , 'updated_message' ), - 10 );
}
2019-11-15 23:26:29 +01:00
2022-12-15 17:41:53 +01:00
if ( ! in_array ( 'sharedaddy' , $active , true ) ) {
add_action ( 'admin_init' , array ( $this -> settings , 'process_update_requests_if_sharedaddy_not_loaded' ) );
add_action ( 'sharing_global_options' , array ( $this -> settings , 'admin_settings_showbuttonon_init' ), 19 );
add_action ( 'sharing_admin_update' , array ( $this -> settings , 'admin_settings_showbuttonon_callback' ), 19 );
2019-11-15 23:26:29 +01:00
add_action ( 'admin_init' , array ( $this -> settings , 'add_meta_box' ) );
2022-12-15 17:41:53 +01:00
} else {
add_filter ( 'sharing_meta_box_title' , array ( $this -> settings , 'add_likes_to_sharing_meta_box_title' ) );
add_action ( 'start_sharing_meta_box_content' , array ( $this -> settings , 'meta_box_content' ) );
2019-11-15 23:26:29 +01:00
}
2021-07-23 11:58:50 +02:00
add_action ( 'admin_init' , array ( $this , 'admin_discussion_likes_settings_init' ) ); // Likes notifications.
2019-11-15 23:26:29 +01:00
add_action ( 'admin_bar_menu' , array ( $this , 'admin_bar_likes' ), 60 );
add_action ( 'wp_enqueue_scripts' , array ( $this , 'load_styles_register_scripts' ) );
add_action ( 'save_post' , array ( $this -> settings , 'meta_box_save' ) );
add_action ( 'edit_attachment' , array ( $this -> settings , 'meta_box_save' ) );
add_action ( 'sharing_global_options' , array ( $this -> settings , 'admin_settings_init' ), 20 );
2021-07-23 11:58:50 +02:00
add_action ( 'sharing_admin_update' , array ( $this -> settings , 'admin_settings_callback' ), 20 );
2019-11-15 23:26:29 +01:00
}
/**
* Set the social_notifications_like option to `on` when the Likes module is activated .
*
* @ since 3.7 . 0
*/
2021-07-23 11:58:50 +02:00
public function set_social_notifications_like () {
2019-11-15 23:26:29 +01:00
update_option ( 'social_notifications_like' , 'on' );
}
/**
* Delete the social_notifications_like option that was set to `on` on module activation .
*
* @ since 3.7 . 0
*/
2021-07-23 11:58:50 +02:00
public function delete_social_notifications_like () {
2019-11-15 23:26:29 +01:00
delete_option ( 'social_notifications_like' );
}
/**
* Overrides default configuration url
*
* @ uses admin_url
* @ return string module settings URL
*/
2021-07-23 11:58:50 +02:00
public function jetpack_likes_configuration_url () {
2019-11-15 23:26:29 +01:00
return admin_url ( 'options-general.php?page=sharing#likes' );
}
/**
* Loads Jetpack ' s CSS on the sharing page so we can use . jetpack - targetable
*/
2021-07-23 11:58:50 +02:00
public function load_jp_css () {
/**
* Do we really need `admin_styles` ? With the new admin UI , it ' s breaking some bits .
* Jetpack :: init () -> admin_styles ();
*/
2019-11-15 23:26:29 +01:00
}
/**
* Load scripts and styles for front end .
*/
2021-07-23 11:58:50 +02:00
public function load_styles_register_scripts () {
2022-12-15 17:41:53 +01:00
wp_enqueue_style ( 'jetpack_likes' , plugins_url ( 'likes/style.css' , __FILE__ ), array (), JETPACK__VERSION );
wp_register_script (
'jetpack_likes_queuehandler' ,
Assets :: get_file_url_for_environment (
'_inc/build/likes/queuehandler.min.js' ,
'modules/likes/queuehandler.js'
),
array (),
JETPACK__VERSION ,
true
);
2019-11-15 23:26:29 +01:00
}
/**
* Adds in the jetpack - targetable class so when we visit sharing #likes our like settings get highlighted by a yellow box
2021-07-23 11:58:50 +02:00
*
* @ param string $html row heading for the sharedaddy " which page " setting .
* @ return string $html with the jetpack - targetable class and likes id . tbody gets closed after the like settings
2019-11-15 23:26:29 +01:00
*/
2021-07-23 11:58:50 +02:00
public function configuration_target_area ( $html = '' ) {
2019-11-15 23:26:29 +01:00
$html = " <tbody id='likes' class='jetpack-targetable'> " . $html ;
return $html ;
}
/**
2021-07-23 11:58:50 +02:00
* 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.
2019-11-15 23:26:29 +01:00
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' );
2021-07-23 11:58:50 +02:00
// Register the setting.
2019-11-15 23:26:29 +01:00
register_setting ( 'discussion' , 'social_notifications_like' , array ( $this , 'admin_discussion_likes_settings_validate' ) );
}
2021-07-23 11:58:50 +02:00
/** 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.
?>
2019-11-15 23:26:29 +01:00
< script type = " text/javascript " >
jQuery ( function ( $ ) {
var table = $ ( '#social_notifications_like' ) . parents ( 'table:first' ),
header = table . prevAll ( 'h2:first' ),
newParent = $ ( '#moderation_notify' ) . parent ( 'label' ) . parent ();
if ( ! table . length || ! header . length || ! newParent . length ) {
return ;
}
newParent . append ( '<br/>' ) . append ( table . end () . parent ( 'label' ) . siblings () . andSelf () );
header . remove ();
table . remove ();
} );
</ script >
2021-07-23 11:58:50 +02:00
< ? php
2019-11-15 23:26:29 +01:00
}
2021-07-23 11:58:50 +02:00
/** Check if email notifications for likes is on or off .
*
* @ param string $option - which option we ' re checking ( social_notifications_like ) .
*/
public function admin_likes_get_option ( $option ) {
2022-12-15 17:41:53 +01:00
$option_setting = get_option ( $option , 'on' );
2019-11-15 23:26:29 +01:00
2021-07-23 11:58:50 +02:00
return ( int ) ( 'on' === $option_setting );
2019-11-15 23:26:29 +01:00
}
2021-07-23 11:58:50 +02:00
/** Display email notification for likes setting in WordPress' discussion settings. */
public function admin_discussion_likes_settings_field () {
2019-11-15 23:26:29 +01:00
$like = $this -> admin_likes_get_option ( 'social_notifications_like' );
2021-07-23 11:58:50 +02:00
?>
2019-11-15 23:26:29 +01:00
< label >< input type = " checkbox " id = " social_notifications_like " name = " social_notifications_like " value = " 1 " < ? php checked ( $like ); ?> /> <?php esc_html_e( 'Someone likes one of my posts', 'jetpack' ); ?></label>
2021-07-23 11:58:50 +02:00
< ? php
2019-11-15 23:26:29 +01:00
}
2021-07-23 11:58:50 +02:00
/**
* Validate email notification settings .
*
* @ param string $input - determines if checbox is on or off .
*/
public function admin_discussion_likes_settings_validate ( $input ) {
2019-11-15 23:26:29 +01:00
// If it's not set (was unchecked during form submission) or was set to off (during option update), return 'off'.
2021-07-23 11:58:50 +02:00
if ( ! $input || 'off' === $input ) {
2019-11-15 23:26:29 +01:00
return 'off' ;
2021-07-23 11:58:50 +02:00
}
// Otherwise return 'on'.
2019-11-15 23:26:29 +01:00
return 'on' ;
}
2021-07-23 11:58:50 +02:00
/** Initialize admin settings */
public function admin_init () {
2019-11-15 23:26:29 +01:00
add_filter ( 'manage_posts_columns' , array ( $this , 'add_like_count_column' ) );
add_filter ( 'manage_pages_columns' , array ( $this , 'add_like_count_column' ) );
add_action ( 'manage_posts_custom_column' , array ( $this , 'likes_edit_column' ), 10 , 2 );
add_action ( 'manage_pages_custom_column' , array ( $this , 'likes_edit_column' ), 10 , 2 );
add_action ( 'admin_print_styles-edit.php' , array ( $this , 'load_admin_css' ) );
2021-07-23 11:58:50 +02:00
add_action ( 'admin_print_scripts-edit.php' , array ( $this , 'enqueue_admin_scripts' ) );
2019-11-15 23:26:29 +01:00
}
2021-07-23 11:58:50 +02:00
/** Initialize action */
public function action_init () {
2022-04-02 10:26:41 +02:00
/*
* Only check if the module is enabled here because
* we are not currently in The Loop and do not yet have access to check
* the switch_like_status post meta flag for the post to be loaded .
*/
if ( is_admin () || ! $this -> settings -> is_likes_module_enabled () ) {
2019-11-15 23:26:29 +01:00
return ;
}
if ( ( defined ( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ||
2021-07-23 11:58:50 +02:00
( defined ( 'APP_REQUEST' ) && APP_REQUEST ) ||
( defined ( 'REST_API_REQUEST' ) && REST_API_REQUEST ) ||
( defined ( 'COOKIE_AUTH_REQUEST' ) && COOKIE_AUTH_REQUEST ) ||
( defined ( 'JABBER_SERVER' ) && JABBER_SERVER ) ) {
2019-11-15 23:26:29 +01:00
return ;
}
if (
class_exists ( 'Jetpack_AMP_Support' )
&& Jetpack_AMP_Support :: is_amp_request ()
) {
return ;
}
2022-12-15 17:41:53 +01:00
add_filter ( 'the_content' , array ( $this , 'post_likes' ), 30 , 1 );
add_filter ( 'the_excerpt' , array ( $this , 'post_likes' ), 30 , 1 );
2019-11-15 23:26:29 +01:00
}
/**
2021-07-23 11:58:50 +02:00
* Load the CSS needed for the wp - admin area .
*/
public function load_admin_css () {
?>
2019-11-15 23:26:29 +01:00
< style type = " text/css " >
. vers img { display : none ; }
. metabox - prefs . vers img { display : inline ; }
. fixed . column - likes { width : 5.5 em ; padding : 8 px 0 ; text - align : left ; }
. fixed . column - stats { width : 5 em ; }
. fixed . column - likes . post - com - count {
- webkit - box - sizing : border - box ;
- moz - box - sizing : border - box ;
box - sizing : border - box ;
display : inline - block ;
padding : 0 8 px ;
height : 2 em ;
margin - top : 5 px ;
- webkit - border - radius : 5 px ;
border - radius : 5 px ;
2021-04-27 08:32:47 +02:00
background - color : #787c82;
2019-11-15 23:26:29 +01:00
color : #FFF;
font - size : 11 px ;
line - height : 21 px ;
}
. fixed . column - likes . post - com - count :: after { border : none ! important ; }
2021-04-27 08:32:47 +02:00
. fixed . column - likes . post - com - count : hover { background - color : #2271b1; }
2019-11-15 23:26:29 +01:00
. fixed . column - likes . vers : before {
font : normal 20 px / 1 dashicons ;
content : '\f155' ;
speak : none ;
- webkit - font - smoothing : antialiased ;
- moz - osx - font - smoothing : grayscale ;
}
@ media screen and ( max - width : 782 px ) {
. fixed . column - likes {
display : none ;
}
}
</ style >
< ? php
}
/**
2021-07-23 11:58:50 +02:00
* Load the JS required for loading the like counts .
*/
public function enqueue_admin_scripts () {
if ( empty ( $_GET [ 'post_type' ] ) || 'post' === $_GET [ 'post_type' ] || 'page' === $_GET [ 'post_type' ] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
2022-12-15 17:41:53 +01:00
wp_enqueue_script (
'likes-post-count' ,
Assets :: get_file_url_for_environment (
'_inc/build/likes/post-count.min.js' ,
'modules/likes/post-count.js'
),
array ( 'jquery' ),
JETPACK__VERSION ,
$in_footer = false
);
wp_enqueue_script (
'likes-post-count-jetpack' ,
Assets :: get_file_url_for_environment (
'_inc/build/likes/post-count-jetpack.min.js' ,
'modules/likes/post-count-jetpack.js'
),
2023-04-26 17:39:43 +02:00
array ( 'jquery' , 'likes-post-count' ),
2022-12-15 17:41:53 +01:00
JETPACK__VERSION ,
$in_footer = false
);
2019-11-15 23:26:29 +01:00
}
}
/**
2021-07-23 11:58:50 +02:00
* Add " Likes " column data to the post edit table in wp - admin .
*
* @ param string $column_name - name of the column .
* @ param int $post_id - the post id .
*/
public function likes_edit_column ( $column_name , $post_id ) {
if ( 'likes' === $column_name ) {
2019-11-15 23:26:29 +01:00
2022-12-15 17:41:53 +01:00
$blog_id = Jetpack_Options :: get_option ( 'id' );
2019-11-15 23:26:29 +01:00
2021-07-23 11:58:50 +02:00
$permalink = get_permalink ( get_the_ID () );
?>
2019-11-15 23:26:29 +01:00
< a title = " " data - post - id = " <?php echo (int) $post_id ; ?> " class = " post-com-count post-like-count " id = " post-like-count-<?php echo (int) $post_id ; ?> " data - blog - id = " <?php echo (int) $blog_id ; ?> " href = " <?php echo esc_url( $permalink ); ?>#like-<?php echo (int) $post_id ; ?> " >
< span class = " comment-count " > 0 </ span >
</ a >
< ? php
}
}
/**
2021-07-23 11:58:50 +02:00
* Add a " Likes " column header to the post edit table in wp - admin .
*
* @ param array $columns - array of columns in wp - admin .
*/
public function add_like_count_column ( $columns ) {
2019-11-15 23:26:29 +01:00
$date = $columns [ 'date' ];
unset ( $columns [ 'date' ] );
$columns [ 'likes' ] = '<span class="vers"><img title="' . esc_attr__ ( 'Likes' , 'jetpack' ) . '" alt="' . esc_attr__ ( 'Likes' , 'jetpack' ) . '" src="//s0.wordpress.com/i/like-grey-icon.png" /><span class="screen-reader-text">' . __ ( 'Likes' , 'jetpack' ) . '</span></span>' ;
2021-07-23 11:58:50 +02:00
$columns [ 'date' ] = $date ;
2019-11-15 23:26:29 +01:00
return $columns ;
}
2021-07-23 11:58:50 +02:00
/**
* Append like button to content .
*
* @ param string $content - content of the page .
*/
public function post_likes ( $content ) {
2022-12-15 17:41:53 +01:00
global $wp_current_filter ;
2019-11-15 23:26:29 +01:00
$post_id = get_the_ID ();
2021-07-23 11:58:50 +02:00
if ( ! is_numeric ( $post_id ) || ! $this -> settings -> is_likes_visible () ) {
2019-11-15 23:26:29 +01:00
return $content ;
2021-07-23 11:58:50 +02:00
}
2019-11-15 23:26:29 +01:00
2022-12-15 17:41:53 +01:00
// 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 ;
2019-11-15 23:26:29 +01:00
}
2022-12-15 17:41:53 +01:00
$blog_id = Jetpack_Options :: get_option ( 'id' );
$url = home_url ();
$url_parts = wp_parse_url ( $url );
$domain = $url_parts [ 'host' ];
2021-07-23 11:58:50 +02:00
// Make sure to include the scripts before the iframe otherwise weird things happen.
2019-11-15 23:26:29 +01:00
add_action ( 'wp_footer' , 'jetpack_likes_master_iframe' , 21 );
/**
2021-07-23 11:58:50 +02:00
* If the same post appears more then once on a page the page goes crazy
2019-11-15 23:26:29 +01:00
* we need a slightly more unique id / name for the widget wrapper .
*/
$uniqid = uniqid ();
2021-07-23 11:58:50 +02:00
$src = sprintf ( 'https://widgets.wp.com/likes/#blog_id=%1$d&post_id=%2$d&origin=%3$s&obj_id=%1$d-%2$d-%4$s' , $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 );
2019-11-15 23:26:29 +01:00
$headline = sprintf (
/** This filter is already documented in modules/sharedaddy/sharing-service.php */
apply_filters ( 'jetpack_sharing_headline_html' , '<h3 class="sd-title">%s</h3>' , esc_html__ ( 'Like this:' , 'jetpack' ), 'likes' ),
esc_html__ ( 'Like this:' , 'jetpack' )
);
2021-08-17 08:33:07 +02:00
$title = esc_html__ ( 'Like or Reblog' , 'jetpack' );
$html = " <div class='sharedaddy sd-block sd-like jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id=' $wrapper ' data-src=' $src ' data-name=' $name ' data-title=' $title '> " ;
2019-11-15 23:26:29 +01:00
$html .= $headline ;
$html .= " <div class='likes-widget-placeholder post-likes-widget-placeholder' style='height: 55px;'><span class='button'><span> " . esc_html__ ( 'Like' , 'jetpack' ) . '</span></span> <span class="loading">' . esc_html__ ( 'Loading...' , 'jetpack' ) . '</span></div>' ;
$html .= " <span class='sd-text-color'></span><a class='sd-link-color'></a> " ;
$html .= '</div>' ;
2021-07-23 11:58:50 +02:00
// Let's make sure that the script is enqueued.
2019-11-15 23:26:29 +01:00
wp_enqueue_script ( 'jetpack_likes_queuehandler' );
return $content . $html ;
}
2021-07-23 11:58:50 +02:00
/** Checks if admin bar is visible.*/
public function is_admin_bar_button_visible () {
2019-11-15 23:26:29 +01:00
global $wp_admin_bar ;
2021-07-23 11:58:50 +02:00
if ( ! is_object ( $wp_admin_bar ) ) {
2019-11-15 23:26:29 +01:00
return false ;
2021-07-23 11:58:50 +02:00
}
2019-11-15 23:26:29 +01:00
2021-07-23 11:58:50 +02:00
if ( ( ! is_singular ( 'post' ) && ! is_attachment () && ! is_page () ) ) {
2019-11-15 23:26:29 +01:00
return false ;
2021-07-23 11:58:50 +02:00
}
2019-11-15 23:26:29 +01:00
2021-07-23 11:58:50 +02:00
if ( ! $this -> settings -> is_likes_visible () ) {
2019-11-15 23:26:29 +01:00
return false ;
2021-07-23 11:58:50 +02:00
}
2019-11-15 23:26:29 +01:00
2021-07-23 11:58:50 +02:00
if ( ! $this -> settings -> is_post_likeable () ) {
2019-11-15 23:26:29 +01:00
return false ;
2021-07-23 11:58:50 +02:00
}
2019-11-15 23:26:29 +01:00
/**
* Filters whether the Like button is enabled in the admin bar .
*
* @ module likes
*
* @ since 2.2 . 0
*
* @ param bool true Should the Like button be visible in the Admin bar . Default to true .
*/
return ( bool ) apply_filters ( 'jetpack_admin_bar_likes_enabled' , true );
}
2021-07-23 11:58:50 +02:00
/** Adds like section in admin bar. */
public function admin_bar_likes () {
2019-11-15 23:26:29 +01:00
global $wp_admin_bar ;
$post_id = get_the_ID ();
if ( ! is_numeric ( $post_id ) || ! $this -> is_admin_bar_button_visible () ) {
return ;
}
$protocol = 'http' ;
2021-07-23 11:58:50 +02:00
if ( is_ssl () ) {
2019-11-15 23:26:29 +01:00
$protocol = 'https' ;
2021-07-23 11:58:50 +02:00
}
2022-12-15 17:41:53 +01:00
$blog_id = Jetpack_Options :: get_option ( 'id' );
$url = home_url ();
$url_parts = wp_parse_url ( $url );
$domain = $url_parts [ 'host' ];
2021-07-23 11:58:50 +02:00
// Make sure to include the scripts before the iframe otherwise weird things happen.
2019-11-15 23:26:29 +01:00
add_action ( 'wp_footer' , 'jetpack_likes_master_iframe' , 21 );
$src = sprintf ( 'https://widgets.wp.com/likes/#blog_id=%2$d&post_id=%3$d&origin=%1$s://%4$s' , $protocol , $blog_id , $post_id , $domain );
$html = " <iframe class='admin-bar-likes-widget jetpack-likes-widget' scrolling='no' frameBorder='0' name='admin-bar-likes-widget' src=' $src '></iframe> " ;
$node = array (
2021-07-23 11:58:50 +02:00
'id' => 'admin-bar-likes-widget' ,
'meta' => array (
'html' => $html ,
),
2019-11-15 23:26:29 +01:00
);
$wp_admin_bar -> add_node ( $node );
}
}
/**
* 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 .
2021-07-23 11:58:50 +02:00
*
* @ param array $post - post data we ' re checking .
2019-11-15 23:26:29 +01:00
*/
function jetpack_post_likes_get_value ( array $post ) {
$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' ) );
2021-07-23 11:58:50 +02:00
// An empty string: post meta was not set, so go with the global setting.
if ( '' === $post_likes_switched ) {
2019-11-15 23:26:29 +01:00
return $sitewide_likes_enabled ;
2021-07-23 11:58:50 +02:00
} elseif ( '0' === $post_likes_switched ) { // User overrode the global setting to disable likes.
2019-11-15 23:26:29 +01:00
return false ;
2021-07-23 11:58:50 +02:00
} elseif ( '1' === $post_likes_switched ) { // User overrode the global setting to enable likes.
2019-11-15 23:26:29 +01:00
return true ;
}
2021-07-23 11:58:50 +02:00
// No default fallback, let's stay explicit.
2019-11-15 23:26:29 +01:00
}
/**
* 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 .
2021-07-23 11:58:50 +02:00
*
* @ param bool $enable_post_likes - checks if post likes are enabled .
* @ param object $post_object - object containing post data .
2019-11-15 23:26:29 +01:00
*/
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 ) {
2021-07-23 11:58:50 +02:00
// Set the meta to 0 if the user wants to disable likes, 1 if user wants to enable.
2019-11-15 23:26:29 +01:00
$switch_like_status = ( $enable_post_likes ? 1 : 0 );
return update_post_meta ( $post_object -> ID , 'switch_like_status' , $switch_like_status );
} else {
2021-07-23 11:58:50 +02:00
// Unset the meta otherwise.
2019-11-15 23:26:29 +01:00
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
2021-07-23 11:58:50 +02:00
// restapi_theme_init because they depend on theme support, so let's also hook to that.
2019-11-15 23:26:29 +01:00
add_action ( 'restapi_theme_init' , 'jetpack_post_likes_register_rest_field' , 20 );
/**
2021-07-23 11:58:50 +02:00
* Set the Likes and Sharing Gutenberg extension availability .
2019-11-15 23:26:29 +01:00
*/
function jetpack_post_likes_set_extension_availability () {
Jetpack_Gutenberg :: set_extension_available ( 'likes' );
}
add_action ( 'jetpack_register_gutenberg_extensions' , 'jetpack_post_likes_set_extension_availability' );
Jetpack_Likes :: init ();