in_jetpack = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? false : true; add_action( 'init', array( &$this, 'action_init' ) ); add_action( 'admin_init', array( $this, 'admin_init' ) ); if ( $this->in_jetpack ) { add_action( 'jetpack_activate_module_likes', array( $this, 'module_toggle' ) ); add_action( 'jetpack_deactivate_module_likes', array( $this, 'module_toggle' ) ); Jetpack::enable_module_configurable( __FILE__ ); Jetpack::module_configuration_load( __FILE__, array( $this, 'configuration_redirect' ) ); 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' ) ); $active = Jetpack::get_active_modules(); if ( ! in_array( 'sharedaddy', $active ) && ! in_array( 'publicize', $active ) ) { add_action( 'admin_menu', array( $this, 'sharing_menu' ) ); // we don't have a sharing page yet } if ( in_array( 'publicize', $active ) && ! in_array( 'sharedaddy', $active ) ) { add_action( 'pre_admin_screen_sharing', array( $this, 'sharing_block' ), 20 ); // we have a sharing page but not the global options area add_action( 'pre_admin_screen_sharing', array( $this, 'updated_message' ), -10 ); } if( ! in_array( 'sharedaddy', $active ) ) { add_action( 'admin_init', array( $this, 'process_update_requests_if_sharedaddy_not_loaded' ) ); add_action( 'sharing_global_options', array( $this, 'admin_settings_showbuttonon_init' ), 19 ); add_action( 'sharing_admin_update', array( $this, 'admin_settings_showbuttonon_callback' ), 19 ); add_action( 'admin_init', array( $this, 'add_meta_box' ) ); } else { add_filter( 'sharing_meta_box_title', array( $this, 'add_likes_to_sharing_meta_box_title' ) ); add_action( 'start_sharing_meta_box_content', array( $this, 'meta_box_content' ) ); } Jetpack_Sync::sync_options( __FILE__, 'social_notifications_like' ); } else { // wpcom add_action( 'admin_init', array( $this, 'add_meta_box' ) ); add_action( 'end_likes_meta_box_content', array( $this, 'sharing_meta_box_content' ) ); add_filter( 'likes_meta_box_title', array( $this, 'add_likes_to_sharing_meta_box_title' ) ); } add_action( 'admin_init', array( $this, 'admin_discussion_likes_settings_init' ) ); // Likes notifications add_action( 'admin_bar_menu', array( $this, 'admin_bar_likes' ), 60 ); add_action( 'save_post', array( $this, 'meta_box_save' ) ); add_action( 'sharing_global_options', array( $this, 'admin_settings_init' ), 20 ); add_action( 'sharing_admin_update', array( $this, 'admin_settings_callback' ), 20 ); } function module_toggle() { $jetpack = Jetpack::init(); $jetpack->sync->register( 'noop' ); } /** * Redirects to the likes section of the sharing page. */ function configuration_redirect() { wp_safe_redirect( admin_url( 'options-general.php?page=sharing#likes' ) ); die(); } /** * Loads Jetpack's CSS on the sharing page so we can use .jetpack-targetable */ function load_jp_css() { Jetpack::init()->admin_styles(); } /** * Adds in the jetpack-targetable class so when we visit sharing#likes our like settings get highlighted by a yellow box * @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 */ function configuration_target_area( $html = '' ) { $html = "" . $html; return $html; } /** * Replaces the "Sharing" title for the post screen metabox with "Likes and Shares" * @param string $title The current title of the metabox, not needed/used. */ function add_likes_to_sharing_meta_box_title( $title ) { return __( 'Likes and Shares', 'jetpack' ); } /** * Adds a metabox to the post screen if the sharing one doesn't currently exist. */ function add_meta_box() { if ( apply_filters( 'post_flair_disable', false ) ) return; $post_types = get_post_types( array( 'public' => true ) ); $title = apply_filters( 'likes_meta_box_title', __( 'Likes', 'jetpack' ) ); foreach( $post_types as $post_type ) { add_meta_box( 'likes_meta', $title, array( $this, 'meta_box_content' ), $post_type, 'advanced', 'high' ); } } function meta_box_save( $post_id ) { if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id; // Record sharing disable. Only needs to be done for WPCOM if ( ! $this->in_jetpack ) { if ( isset( $_POST['post_type'] ) && ( 'post' == $_POST['post_type'] || 'page' == $_POST['post_type'] ) ) { if ( isset( $_POST['wpl_sharing_status_hidden'] ) && !isset( $_POST['wpl_enable_post_sharing'] ) ) { update_post_meta( $post_id, 'sharing_disabled', 1 ); } else { delete_post_meta( $post_id, 'sharing_disabled' ); } } } if ( empty( $_POST['wpl_like_status_hidden'] ) ) return $post_id; if ( 'post' == $_POST['post_type'] ) { if ( !current_user_can( 'edit_post', $post_id ) ) { return $post_id; } } // Record a change in like status for this post - only if it contradicts the // site like setting. if ( ( $this->is_enabled_sitewide() && empty( $_POST['wpl_enable_post_likes'] ) ) || ( ! $this->is_enabled_sitewide() && !empty( $_POST['wpl_enable_post_likes'] ) ) ) { update_post_meta( $post_id, 'switch_like_status', 1 ); //$g_gif = file_get_contents( 'http://stats.wordpress.com/g.gif?v=wpcom-no-pv&x_likes=switched_post_like_status' ); @todo stat } else { delete_post_meta( $post_id, 'switch_like_status' ); } return $post_id; } /** * Shows the likes option in the post screen metabox. */ function meta_box_content( $post ) { $post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID(); $checked = true; $disabled = ! $this->is_enabled_sitewide(); $switched_status = get_post_meta( $post_id, 'switch_like_status', true ); if ( $disabled && empty( $switched_status ) || false == $disabled && !empty( $switched_status ) ) $checked = false; do_action( 'start_likes_meta_box_content', $post ); ?>

ID ) ? (int) $post->ID : get_the_ID(); $disabled = get_post_meta( $post_id, 'sharing_disabled', true ); ?>

admin_likes_get_option( 'social_notifications_like' ); ?>
*/ ?> ' ); ?> true ) ) ); array_unshift( $shows, 'index' ); $global = $this->get_options(); foreach ( $shows as $show ) : if ( 'index' == $show ) { $label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' ); } else { $post_type_object = get_post_type_object( $show ); $label = $post_type_object->labels->name; } ?> '; ?> ' ); ?> true ) ) ); $shows[] = 'index'; $data = $_POST; if ( isset( $data['show'] ) ) { if ( is_scalar( $data['show'] ) ) { switch ( $data['show'] ) { case 'posts' : $data['show'] = array( 'post', 'page' ); break; case 'index' : $data['show'] = array( 'index' ); break; case 'posts-index' : $data['show'] = array( 'post', 'page', 'index' ); break; } } if ( $data['show'] = array_intersect( $data['show'], $shows ) ) { $options['global']['show'] = $data['show']; } } else { $options['global']['show'] = array(); } update_option( 'sharing-options', $options ); } /** * Adds the admin update hook so we can save settings even if Sharedaddy is not enabled. */ function process_update_requests_if_sharedaddy_not_loaded() { if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'sharing.php' || $_GET['page'] == 'sharing' ) ) { if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) { do_action( 'sharing_admin_update' ); wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) ); die(); } } } /** * Saves the setting in the database, bumps a stat on WordPress.com */ function admin_settings_callback() { // We're looking for these, and doing a dance to set some stats and save // them together in array option. $new_state = !empty( $_POST['wpl_default'] ) ? $_POST['wpl_default'] : 'on'; $db_state = $this->is_enabled_sitewide(); /** Default State *********************************************************/ // Checked (enabled) switch( $new_state ) { case 'off' : if ( true == $db_state && ! $this->in_jetpack ) { $g_gif = file_get_contents( 'http://stats.wordpress.com/g.gif?v=wpcom-no-pv&x_likes=disabled_likes' ); } update_option( 'disabled_likes', 1 ); break; case 'on' : default: if ( false == $db_state && ! $this->in_jetpack ) { $g_gif = file_get_contents( 'http://stats.wordpress.com/g.gif?v=wpcom-no-pv&x_likes=reenabled_likes' ); } delete_option( 'disabled_likes' ); break; } // comment setting $new_comments_state = !empty( $_POST['jetpack_comment_likes_enabled'] ) ? $_POST['jetpack_comment_likes_enabled'] : false; switch( (bool) $new_comments_state ) { case true: update_option( 'jetpack_comment_likes_enabled', 1 ); break; case false: default: update_option( 'jetpack_comment_likes_enabled', 0 ); break; } } /** * Adds the 'sharing' menu to the settings menu. * Only ran if sharedaddy and publicize are not already active. */ function sharing_menu() { add_submenu_page( 'options-general.php', esc_html__( 'Sharing Settings', 'jetpack' ), esc_html__( 'Sharing', 'jetpack' ), 'manage_options', 'sharing', array( $this, 'sharing_page' ) ); } /** * Provides a sharing page with the sharing_global_options hook * so we can display the setting. * Only ran if sharedaddy and publicize are not already active. */ function sharing_page() { $this->updated_message(); ?>

sharing_block(); ?>

' . esc_html__( 'Settings have been saved', 'jetpack' ) . '

'; } /** * Returns just the "sharing buttons" w/ like option block, so it can be inserted into different sharing page contexts */ function sharing_block() { ?>

in_jetpack ) { add_filter( 'the_content', array( &$this, 'post_likes' ), 30, 1 ); wp_enqueue_script( 'postmessage', plugins_url( '_inc/postmessage.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); wp_enqueue_script( 'jquery_inview', plugins_url( '_inc/jquery.inview.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); wp_enqueue_script( 'jetpack_resize', plugins_url( '_inc/jquery.jetpack-resize.js' , dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array(), JETPACK__VERSION ); } else { add_filter( 'post_flair', array( &$this, 'post_likes' ), 30, 1 ); add_filter( 'post_flair_block_css', array( $this, 'post_flair_service_enabled_like' ) ); wp_enqueue_script( 'postmessage', '/wp-content/js/postmessage.js', array( 'jquery' ), JETPACK__VERSION, false ); wp_enqueue_script( 'jquery_inview', '/wp-content/js/jquery/jquery.inview.js', array( 'jquery' ), JETPACK__VERSION, false ); wp_enqueue_script( 'jetpack_resize', '/wp-content/js/jquery/jquery.jetpack-resize.js', array( 'jquery' ), JETPACK__VERSION, false ); wp_enqueue_style( 'jetpack_likes', plugins_url( 'jetpack-likes.css', __FILE__ ), array(), JETPACK__VERSION ); } } /** * Load the CSS needed for the wp-admin area. */ function load_admin_css() { ?> in_jetpack ) { wp_enqueue_script( 'likes-post-count', plugins_url( 'modules/likes/post-count.js', dirname( __FILE__ ) ), array( 'jquery' ), JETPACK__VERSION ); wp_enqueue_script( 'likes-post-count-jetpack', plugins_url( 'modules/likes/post-count-jetpack.js', dirname( __FILE__ ) ), array( 'likes-post-count' ), JETPACK__VERSION ); } else { wp_enqueue_script( 'jquery.wpcom-proxy-request', "/wp-content/js/jquery/jquery.wpcom-proxy-request.js", array('jquery'), NULL, true ); wp_enqueue_script( 'likes-post-count', plugins_url( 'likes/post-count.js', dirname( __FILE__ ) ), array( 'jquery' ), JETPACK__VERSION ); wp_enqueue_script( 'likes-post-count-wpcom', plugins_url( 'likes/post-count-wpcom.js', dirname( __FILE__ ) ), array( 'likes-post-count', 'jquery.wpcom-proxy-request' ), JETPACK__VERSION ); } } } /** * Add "Likes" column data to the post edit table in wp-admin. * * @param string $column_name * @param int $post_id */ function likes_edit_column( $column_name, $post_id ) { if ( 'likes' == $column_name ) { if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { $blog_id = get_current_blog_id(); } else { $blog_id = Jetpack_Options::get_option( 'id' ); } $permalink = get_permalink( get_the_ID() ); ?> 0 ' . esc_attr__( 'Likes', 'jetpack' ) . ''; $columns['date'] = $date; return $columns; } function post_likes( $content ) { global $post; if ( ! $this->is_likes_visible() ) return $content; if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { $blog_id = get_current_blog_id(); $bloginfo = get_blog_details( (int) $blog_id ); $domain = $bloginfo->domain; } else { $blog_id = Jetpack_Options::get_option( 'id' ); $url = home_url(); $url_parts = parse_url( $url ); $domain = $url_parts['host']; } add_filter( 'wp_footer', array( $this, 'likes_master' ) ); /** * 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( '//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 ); $html = "

" . esc_html__( 'Like this:', 'jetpack' ) . '

'; $html .= "
" . esc_html__( 'Like', 'jetpack' ) . ' ' . esc_html__( 'Loading...', 'jetpack' ) . '
'; $html .= ""; $html .= '
'; return $content . $html; } function comment_likes( $content, $comment = null ) { if ( empty( $comment ) ) return $content; if ( ! $this->is_comments_enabled() ) return $content; $protocol = 'http'; if ( is_ssl() ) $protocol = 'https'; if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { $blog_id = get_current_blog_id(); $bloginfo = get_blog_details( (int) $blog_id ); $domain = $bloginfo->domain; } else { $blog_id = Jetpack_Options::get_option( 'id' ); $url = home_url(); $url_parts = parse_url( $url ); $domain = $url_parts['host']; } add_filter( 'wp_footer', array( $this, 'likes_master' ) ); $src = sprintf( '%1$s://widgets.wp.com/likes/#blog_id=%2$d&comment_id=%3$d&origin=%1$s://%4$s', $protocol, $blog_id, $comment->comment_ID, $domain ); $name = sprintf( 'like-comment-frame-%1$d-%2$d', $blog_id, $comment->comment_ID ); $wrapper = sprintf( 'like-comment-wrapper-%1$d-%2$d', $blog_id, $comment->comment_ID ); $html = "
"; $html .= ""; $html .= '
'; return $content . $html; } function post_flair_service_enabled_like( $classes ) { $classes[] = 'sd-like-enabled'; return $classes; } function admin_bar_likes() { global $wp_admin_bar, $post; if ( ! $this->is_admin_bar_button_visible() ) { return; } $protocol = 'http'; if ( is_ssl() ) $protocol = 'https'; if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { $blog_id = get_current_blog_id(); $bloginfo = get_blog_details( (int) $blog_id ); $domain = $bloginfo->domain; } else { $blog_id = Jetpack_Options::get_option( 'id' ); $url = home_url(); $url_parts = parse_url( $url ); $domain = $url_parts['host']; } add_filter( 'wp_footer', array( $this, 'likes_master' ) ); $src = sprintf( '%1$s://widgets.wp.com/likes/#blog_id=%2$d&post_id=%3$d&origin=%1$s://%4$s', $protocol, $blog_id, $post->ID, $domain ); $html = ""; $node = array( 'id' => 'admin-bar-likes-widget', 'meta' => array( 'html' => $html ) ); $wp_admin_bar->add_node( $node ); } function likes_master() { $protocol = 'http'; if ( is_ssl() ) $protocol = 'https'; $locale = ( '' == get_locale() || 'en' == get_locale() ) ? '' : '&lang=' . strtolower( substr( get_locale(), 0, 2 ) ); $src = sprintf( '%1$s://widgets.wp.com/likes/master.html?ver=%2$s#ver=%2$s%3$s&mp6=%4$d', $protocol, $this->version, $locale, apply_filters( 'mp6_enabled', 0 ) ); $likersText = wp_kses( __( '%d bloggers like this:', 'jetpack' ), array( 'span' => array() ) ); ?>
is_post_likeable(); } else { // Otherwise, check and see if likes are enabled sitewide $enabled = $this->is_enabled_sitewide(); } if ( post_password_required() ) $enabled = false; /** Other Checks ******************************************************/ // Do not show on excerpts if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) { $enabled = false; // Sharing Setting Overrides **************************************** } else { // Single post if ( is_singular( 'post' ) ) { if ( ! $this->is_single_post_enabled() ) { $enabled = false; } // Single page } elseif ( is_page() ) { if ( ! $this->is_single_page_enabled() ) { $enabled = false; } // Attachment } elseif ( is_attachment() ) { if ( ! $this->is_attachment_enabled() ) { $enabled = false; } // All other loops } elseif ( ! $this->is_index_enabled() ) { $enabled = false; } } } // Check that the post is a public, published post. if ( 'publish' != $post->post_status ) { $enabled = false; } // Run through the sharing filters $enabled = apply_filters( 'sharing_show', $enabled, $post ); return (bool) apply_filters( 'wpl_is_likes_visible', $enabled ); } /** * Returns the current state of the "WordPress.com Likes are" option. * @return boolean true if enabled sitewide, false if not */ function is_enabled_sitewide() { return (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) ); } /** * Returns if comment likes are enabled. Defaults to 'on' * @todo decide what the default should be * @return boolean true if we should show comment likes, false if not */ function is_comments_enabled() { return (bool) apply_filters( 'jetpack_comment_likes_enabled', get_option( 'jetpack_comment_likes_enabled', true ) ); } function is_admin_bar_button_visible() { global $wp_admin_bar; if ( ! is_object( $wp_admin_bar ) ) return false; if ( ( ! is_singular( 'post' ) && ! is_attachment() && ! is_page() ) ) return false; if ( ! $this->is_likes_visible() ) return false; if ( ! $this->is_post_likeable() ) return false; return (bool) apply_filters( 'jetpack_admin_bar_likes_enabled', true ); } /** * Are likes enabled for this post? * * @param int $post_id * @retun bool */ function is_post_likeable( $post_id = 0 ) { $post = get_post( $post_id ); if ( !$post || is_wp_error( $post ) ) { return false; } $sitewide_likes_enabled = (bool) Jetpack_Likes::is_enabled_sitewide(); $post_likes_switched = (bool) get_post_meta( $post->ID, 'switch_like_status', true ); $post_likes_enabled = $sitewide_likes_enabled; if ( $post_likes_switched ) { $post_likes_enabled = ! $post_likes_enabled; } return $post_likes_enabled; } /** * Are Post Likes enabled on archive/front/search pages? * * @return bool */ function is_index_enabled() { $options = $this->get_options(); return (bool) apply_filters( 'wpl_is_index_disabled', (bool) in_array( 'index', $options['show'] ) ); } /** * Are Post Likes enabled on single posts? * * @return bool */ function is_single_post_enabled() { $options = $this->get_options(); return (bool) apply_filters( 'wpl_is_single_post_disabled', (bool) in_array( 'post', $options['show'] ) ); } /** * Are Post Likes enabled on single pages? * * @return bool */ function is_single_page_enabled() { $options = $this->get_options(); return (bool) apply_filters( 'wpl_is_single_page_disabled', (bool) in_array( 'page', $options['show'] ) ); } /** * Are Media Likes enabled on single pages? * * @return bool */ function is_attachment_enabled() { $options = $this->get_options(); return (bool) apply_filters( 'wpl_is_attachment_disabled', (bool) in_array( 'attachment', $options['show'] ) ); } } Jetpack_Likes::init();