' . Jetpack::get_jp_emblem() . ''; } /** * Prepare actions according to screen and post type. * * @since 3.8.2 * * @uses Jetpack_Autoupdate::get_possible_failures() * * @param object $screen */ function prepare_jitms( $screen ) { if ( ! in_array( $screen->id, array( 'toplevel_page_jetpack', 'jetpack_page_stats', 'jetpack_page_akismet-key-config', 'admin_page_jetpack_modules' ) ) ) { add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) ); add_action( 'admin_notices', array( $this, 'ajax_message' ) ); add_action( 'edit_form_top', array( $this, 'ajax_message' ) ); } } /** * A special filter for WooCommerce, to set a message based on local state. * * @param $message string The current message * * @return array The new message */ static function jitm_woocommerce_services_msg( $content ) { if ( ! function_exists( 'wc_get_base_location' ) ) { return $content; } $base_location = wc_get_base_location(); switch ( $base_location['country'] ) { case 'US': $content->message = esc_html__( 'New free service: Show USPS shipping rates on your store! Added bonus: print shipping labels without leaving WooCommerce.', 'jetpack' ); break; case 'CA': $content->message = esc_html__( 'New free service: Show Canada Post shipping rates on your store!', 'jetpack' ); break; default: $content->message = ''; } return $content; } /** * A special filter for WooCommerce Call To Action button * * @param $CTA string The existing CTA * * @return string The new CTA */ static function jitm_jetpack_woo_services_install( $CTA ) { return wp_nonce_url( add_query_arg( array( 'wc-services-action' => 'install' ), admin_url( 'admin.php?page=wc-settings' ) ), 'wc-services-install' ); } /** * A special filter for WooCommerce Call To Action button * * @param $CTA string The existing CTA * * @return string The new CTA */ static function jitm_jetpack_woo_services_activate( $CTA ) { return wp_nonce_url( add_query_arg( array( 'wc-services-action' => 'activate' ), admin_url( 'admin.php?page=wc-settings' ) ), 'wc-services-install' ); } /** * Injects the dom to show a JITM inside of */ function ajax_message() { $message_path = $this->get_message_path(); $query_string = _http_build_query( $_GET, '', ',' ); ?>
id . ':' . current_filter(); } /** * Function to enqueue jitm css and js */ function jitm_enqueue_files() { $min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; wp_register_style( 'jetpack-jitm-css', plugins_url( "css/jetpack-admin-jitm{$min}.css", JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION . '-201243242' ); wp_style_add_data( 'jetpack-jitm-css', 'rtl', 'replace' ); wp_style_add_data( 'jetpack-jitm-css', 'suffix', $min ); wp_enqueue_style( 'jetpack-jitm-css' ); wp_enqueue_script( 'jetpack-jitm-new', plugins_url( '_inc/jetpack-jitm.js', JETPACK__PLUGIN_FILE ), array( 'jquery' ), JETPACK__VERSION, true ); wp_localize_script( 'jetpack-jitm-new', 'jitm_config', array( 'api_root' => esc_url_raw( rest_url() ), ) ); } /** * Dismisses a JITM feature class so that it will no longer be shown * * @param $id string The id of the JITM that was dismissed * @param $feature_class string The feature class of the JITM that was dismissed * * @return bool Always true */ function dismiss( $id, $feature_class ) { JetpackTracking::record_user_event( 'jitm_dismiss_client', array( 'jitm_id' => $id, 'feature_class' => $feature_class, ) ); $hide_jitm = Jetpack_Options::get_option( 'hide_jitm' ); if ( ! is_array( $hide_jitm ) ) { $hide_jitm = array(); } if ( isset( $hide_jitm[ $feature_class ] ) ) { if ( ! is_array( $hide_jitm[ $feature_class ] ) ) { $hide_jitm[ $feature_class ] = array( 'last_dismissal' => 0, 'number' => 0 ); } } else { $hide_jitm[ $feature_class ] = array( 'last_dismissal' => 0, 'number' => 0 ); } $number = $hide_jitm[ $feature_class ]['number']; $hide_jitm[ $feature_class ] = array( 'last_dismissal' => time(), 'number' => $number + 1 ); Jetpack_Options::update_option( 'hide_jitm', $hide_jitm ); return true; } /** * Asks the wpcom API for the current message to display keyed on query string and message path * * @param $message_path string The message path to ask for * @param $query string The query string originally from the front end * * @return array The JITM's to show, or an empty array if there is nothing to show */ function get_messages( $message_path, $query ) { // custom filters go here add_filter( 'jitm_woocommerce_services_msg', array( 'Jetpack_JITM', 'jitm_woocommerce_services_msg' ) ); add_filter( 'jitm_jetpack_woo_services_install', array( 'Jetpack_JITM', 'jitm_jetpack_woo_services_install' ) ); add_filter( 'jitm_jetpack_woo_services_activate', array( 'Jetpack_JITM', 'jitm_jetpack_woo_services_activate' ) ); $user = wp_get_current_user(); // unauthenticated or invalid requests just bail if ( ! $user ) { return array(); } require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-client.php' ); $site_id = Jetpack_Options::get_option( 'id' ); // build our jitm request $path = add_query_arg( array( 'external_user_id' => urlencode_deep( $user->ID ), 'query_string' => urlencode_deep( $query ), ), sprintf( '/sites/%d/jitm/%s', $site_id, $message_path ) ); // attempt to get from cache $envelopes = get_transient( 'jetpack_jitm_' . substr( md5( $path ), 0, 31 ) ); // if something is in the cache and it was put in the cache after the last sync we care about, use it $use_cache = false; /** This filter is documented in class.jetpack.php */ if ( apply_filters( 'jetpack_just_in_time_msg_cache', false ) ) { $use_cache = true; } if ( $use_cache ) { $last_sync = (int) get_transient( 'jetpack_last_plugin_sync' ); $from_cache = $envelopes && $last_sync > 0 && $last_sync < $envelopes['last_response_time']; } else { $from_cache = false; } // otherwise, ask again if ( ! $from_cache ) { $wpcom_response = Jetpack_Client::wpcom_json_api_request_as_blog( $path, '2', array( 'user_id' => $user->ID, 'user_roles' => implode( ',', $user->roles ), ), null, 'wpcom' ); // silently fail...might be helpful to track it? if ( is_wp_error( $wpcom_response ) ) { return array(); } $envelopes = json_decode( $wpcom_response['body'] ); if ( ! is_array( $envelopes ) ) { return array(); } $expiration = isset( $envelopes[0] ) ? $envelopes[0]->ttl : 300; // do not cache if expiration is 0 or we're not using the cache if ( 0 != $expiration && $use_cache ) { $envelopes['last_response_time'] = time(); set_transient( 'jetpack_jitm_' . substr( md5( $path ), 0, 31 ), $envelopes, $expiration ); } } $hidden_jitms = Jetpack_Options::get_option( 'hide_jitm' ); unset( $envelopes['last_response_time'] ); foreach ( $envelopes as $idx => &$envelope ) { $dismissed_feature = isset( $hidden_jitms[ $envelope->feature_class ] ) && is_array( $hidden_jitms[ $envelope->feature_class ] ) ? $hidden_jitms[ $envelope->feature_class ] : null; // if the this feature class has been dismissed and the request has not passed the ttl, skip it as it's been dismissed if ( is_array( $dismissed_feature ) && ( time() - $dismissed_feature['last_dismissal'] < $envelope->expires || $dismissed_feature['number'] >= $envelope->max_dismissal ) ) { unset( $envelopes[ $idx ] ); continue; } JetpackTracking::record_user_event( 'jitm_view_client', array( 'jitm_id' => $envelope->id, ) ); $normalized_site_url = Jetpack::build_raw_urls( get_home_url() ); $envelope->url = 'https://jetpack.com/redirect/?source=jitm-' . $envelope->id . '&site=' . $normalized_site_url . '&u=' . $user->ID; $envelope->jitm_stats_url = Jetpack::build_stats_url( array( 'x_jetpack-jitm' => $envelope->id ) ); if ( $envelope->CTA->hook ) { $envelope->url = apply_filters( 'jitm_' . $envelope->CTA->hook, $envelope->url ); unset( $envelope->CTA->hook ); } if ( isset( $envelope->content->hook ) ) { $envelope->content = apply_filters( 'jitm_' . $envelope->content->hook, $envelope->content ); unset( $envelope->content->hook ); } // no point in showing an empty message if ( empty( $envelope->content->message ) ) { return array(); } switch ( $envelope->content->icon ) { case 'jetpack': $envelope->content->icon = '
' . Jetpack::get_jp_emblem() . '
'; break; case 'woocommerce': $envelope->content->icon = '
'; break; default: $envelope->content->icon = ''; break; } $jetpack = Jetpack::init(); $jetpack->stat( 'jitm', $envelope->id . '-viewed-' . JETPACK__VERSION ); $jetpack->do_stats( 'server_side' ); } return $envelopes; } } add_action( 'init', array( 'Jetpack_JITM', 'init' ) );