class.jetpack-jitm.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. /**
  3. * Jetpack just in time messaging through out the admin
  4. *
  5. * @since 3.7.0
  6. */
  7. class Jetpack_JITM {
  8. /**
  9. * @var Jetpack_JITM
  10. **/
  11. private static $instance = null;
  12. /**
  13. * Initializes the class, or returns the singleton
  14. *
  15. * @return Jetpack_JITM | false
  16. */
  17. static function init() {
  18. /**
  19. * Filter to turn off all just in time messages
  20. *
  21. * @since 3.7.0
  22. * @since 5.4.0 Correct docblock to reflect default arg value
  23. *
  24. * @param bool false Whether to show just in time messages.
  25. */
  26. if ( ! apply_filters( 'jetpack_just_in_time_msgs', false ) ) {
  27. return false;
  28. }
  29. if ( is_null( self::$instance ) ) {
  30. self::$instance = new Jetpack_JITM;
  31. }
  32. return self::$instance;
  33. }
  34. /**
  35. * Jetpack_JITM constructor.
  36. */
  37. private function __construct() {
  38. if ( ! Jetpack::is_active() || Jetpack::is_development_mode() ) {
  39. return;
  40. }
  41. add_action( 'current_screen', array( $this, 'prepare_jitms' ) );
  42. }
  43. /**
  44. * Get's the Jetpack emblem
  45. *
  46. * @return string The Jetpack emblem
  47. */
  48. function get_emblem() {
  49. return '<div class="jp-emblem">' . Jetpack::get_jp_emblem() . '</div>';
  50. }
  51. /**
  52. * Prepare actions according to screen and post type.
  53. *
  54. * @since 3.8.2
  55. *
  56. * @uses Jetpack_Autoupdate::get_possible_failures()
  57. *
  58. * @param object $screen
  59. */
  60. function prepare_jitms( $screen ) {
  61. if ( ! in_array( $screen->id, array(
  62. 'jetpack_page_stats',
  63. 'jetpack_page_akismet-key-config',
  64. 'admin_page_jetpack_modules'
  65. ) ) ) {
  66. add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
  67. add_action( 'admin_notices', array( $this, 'ajax_message' ) );
  68. add_action( 'edit_form_top', array( $this, 'ajax_message' ) );
  69. }
  70. }
  71. /**
  72. * A special filter for WooCommerce, to set a message based on local state.
  73. *
  74. * @param $message string The current message
  75. *
  76. * @return array The new message
  77. */
  78. static function jitm_woocommerce_services_msg( $content ) {
  79. if ( ! function_exists( 'wc_get_base_location' ) ) {
  80. return $content;
  81. }
  82. $base_location = wc_get_base_location();
  83. switch ( $base_location['country'] ) {
  84. case 'US':
  85. $content->message = esc_html__( 'New free service: Show USPS shipping rates on your store! Added bonus: print shipping labels without leaving WooCommerce.', 'jetpack' );
  86. break;
  87. case 'CA':
  88. $content->message = esc_html__( 'New free service: Show Canada Post shipping rates on your store!', 'jetpack' );
  89. break;
  90. default:
  91. $content->message = '';
  92. }
  93. return $content;
  94. }
  95. /**
  96. * A special filter for WooCommerce Call To Action button
  97. *
  98. * @param $CTA string The existing CTA
  99. *
  100. * @return string The new CTA
  101. */
  102. static function jitm_jetpack_woo_services_install( $CTA ) {
  103. return wp_nonce_url( add_query_arg( array(
  104. 'wc-services-action' => 'install'
  105. ), admin_url( 'admin.php?page=wc-settings' ) ), 'wc-services-install' );
  106. }
  107. /**
  108. * A special filter for WooCommerce Call To Action button
  109. *
  110. * @param $CTA string The existing CTA
  111. *
  112. * @return string The new CTA
  113. */
  114. static function jitm_jetpack_woo_services_activate( $CTA ) {
  115. return wp_nonce_url( add_query_arg( array(
  116. 'wc-services-action' => 'activate'
  117. ), admin_url( 'admin.php?page=wc-settings' ) ), 'wc-services-install' );
  118. }
  119. /**
  120. * Injects the dom to show a JITM inside of
  121. */
  122. function ajax_message() {
  123. $message_path = $this->get_message_path();
  124. $query_string = _http_build_query( $_GET, '', ',' );
  125. $current_screen = wp_unslash( $_SERVER['REQUEST_URI'] );
  126. ?>
  127. <div class="jetpack-jitm-message"
  128. data-nonce="<?php echo wp_create_nonce( 'wp_rest' ) ?>"
  129. data-message-path="<?php echo esc_attr( $message_path ) ?>"
  130. data-query="<?php echo urlencode_deep( $query_string ) ?>"
  131. data-redirect="<?php echo urlencode_deep( $current_screen ) ?>"
  132. ></div>
  133. <?php
  134. }
  135. /**
  136. * Get's the current message path for display of a JITM
  137. *
  138. * @return string The message path
  139. */
  140. function get_message_path() {
  141. $screen = get_current_screen();
  142. return 'wp:' . $screen->id . ':' . current_filter();
  143. }
  144. /**
  145. * Function to enqueue jitm css and js
  146. */
  147. function jitm_enqueue_files() {
  148. $min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
  149. wp_register_style(
  150. 'jetpack-jitm-css',
  151. plugins_url( "css/jetpack-admin-jitm{$min}.css", JETPACK__PLUGIN_FILE ),
  152. false,
  153. JETPACK__VERSION .
  154. '-201243242'
  155. );
  156. wp_style_add_data( 'jetpack-jitm-css', 'rtl', 'replace' );
  157. wp_style_add_data( 'jetpack-jitm-css', 'suffix', $min );
  158. wp_enqueue_style( 'jetpack-jitm-css' );
  159. wp_enqueue_script(
  160. 'jetpack-jitm-new',
  161. Jetpack::get_file_url_for_environment( '_inc/build/jetpack-jitm.min.js', '_inc/jetpack-jitm.js' ),
  162. array( 'jquery' ),
  163. JETPACK__VERSION,
  164. true
  165. );
  166. wp_localize_script( 'jetpack-jitm-new', 'jitm_config', array(
  167. 'api_root' => esc_url_raw( rest_url() ),
  168. ) );
  169. }
  170. /**
  171. * Dismisses a JITM feature class so that it will no longer be shown
  172. *
  173. * @param $id string The id of the JITM that was dismissed
  174. * @param $feature_class string The feature class of the JITM that was dismissed
  175. *
  176. * @return bool Always true
  177. */
  178. function dismiss( $id, $feature_class ) {
  179. JetpackTracking::record_user_event( 'jitm_dismiss_client', array(
  180. 'jitm_id' => $id,
  181. 'feature_class' => $feature_class,
  182. ) );
  183. $hide_jitm = Jetpack_Options::get_option( 'hide_jitm' );
  184. if ( ! is_array( $hide_jitm ) ) {
  185. $hide_jitm = array();
  186. }
  187. if ( isset( $hide_jitm[ $feature_class ] ) ) {
  188. if ( ! is_array( $hide_jitm[ $feature_class ] ) ) {
  189. $hide_jitm[ $feature_class ] = array( 'last_dismissal' => 0, 'number' => 0 );
  190. }
  191. } else {
  192. $hide_jitm[ $feature_class ] = array( 'last_dismissal' => 0, 'number' => 0 );
  193. }
  194. $number = $hide_jitm[ $feature_class ]['number'];
  195. $hide_jitm[ $feature_class ] = array( 'last_dismissal' => time(), 'number' => $number + 1 );
  196. Jetpack_Options::update_option( 'hide_jitm', $hide_jitm );
  197. return true;
  198. }
  199. /**
  200. * Asks the wpcom API for the current message to display keyed on query string and message path
  201. *
  202. * @param $message_path string The message path to ask for
  203. * @param $query string The query string originally from the front end
  204. *
  205. * @return array The JITM's to show, or an empty array if there is nothing to show
  206. */
  207. function get_messages( $message_path, $query ) {
  208. // custom filters go here
  209. add_filter( 'jitm_woocommerce_services_msg', array( 'Jetpack_JITM', 'jitm_woocommerce_services_msg' ) );
  210. add_filter( 'jitm_jetpack_woo_services_install', array( 'Jetpack_JITM', 'jitm_jetpack_woo_services_install' ) );
  211. add_filter( 'jitm_jetpack_woo_services_activate', array(
  212. 'Jetpack_JITM',
  213. 'jitm_jetpack_woo_services_activate'
  214. ) );
  215. $user = wp_get_current_user();
  216. // unauthenticated or invalid requests just bail
  217. if ( ! $user ) {
  218. return array();
  219. }
  220. require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-client.php' );
  221. $site_id = Jetpack_Options::get_option( 'id' );
  222. // build our jitm request
  223. $path = add_query_arg( array(
  224. 'external_user_id' => urlencode_deep( $user->ID ),
  225. 'query_string' => urlencode_deep( $query ),
  226. ), sprintf( '/sites/%d/jitm/%s', $site_id, $message_path ) );
  227. // attempt to get from cache
  228. $envelopes = get_transient( 'jetpack_jitm_' . substr( md5( $path ), 0, 31 ) );
  229. // if something is in the cache and it was put in the cache after the last sync we care about, use it
  230. $use_cache = false;
  231. /** This filter is documented in class.jetpack.php */
  232. if ( apply_filters( 'jetpack_just_in_time_msg_cache', false ) ) {
  233. $use_cache = true;
  234. }
  235. if ( $use_cache ) {
  236. $last_sync = (int) get_transient( 'jetpack_last_plugin_sync' );
  237. $from_cache = $envelopes && $last_sync > 0 && $last_sync < $envelopes['last_response_time'];
  238. } else {
  239. $from_cache = false;
  240. }
  241. // otherwise, ask again
  242. if ( ! $from_cache ) {
  243. $wpcom_response = Jetpack_Client::wpcom_json_api_request_as_blog(
  244. $path,
  245. '2',
  246. array(
  247. 'user_id' => $user->ID,
  248. 'user_roles' => implode( ',', $user->roles ),
  249. ),
  250. null,
  251. 'wpcom'
  252. );
  253. // silently fail...might be helpful to track it?
  254. if ( is_wp_error( $wpcom_response ) ) {
  255. return array();
  256. }
  257. $envelopes = json_decode( $wpcom_response['body'] );
  258. if ( ! is_array( $envelopes ) ) {
  259. return array();
  260. }
  261. $expiration = isset( $envelopes[0] ) ? $envelopes[0]->ttl : 300;
  262. // do not cache if expiration is 0 or we're not using the cache
  263. if ( 0 != $expiration && $use_cache ) {
  264. $envelopes['last_response_time'] = time();
  265. set_transient( 'jetpack_jitm_' . substr( md5( $path ), 0, 31 ), $envelopes, $expiration );
  266. }
  267. }
  268. $hidden_jitms = Jetpack_Options::get_option( 'hide_jitm' );
  269. unset( $envelopes['last_response_time'] );
  270. foreach ( $envelopes as $idx => &$envelope ) {
  271. $dismissed_feature = isset( $hidden_jitms[ $envelope->feature_class ] ) && is_array( $hidden_jitms[ $envelope->feature_class ] ) ? $hidden_jitms[ $envelope->feature_class ] : null;
  272. // if the this feature class has been dismissed and the request has not passed the ttl, skip it as it's been dismissed
  273. if ( is_array( $dismissed_feature ) && ( time() - $dismissed_feature['last_dismissal'] < $envelope->expires || $dismissed_feature['number'] >= $envelope->max_dismissal ) ) {
  274. unset( $envelopes[ $idx ] );
  275. continue;
  276. }
  277. JetpackTracking::record_user_event( 'jitm_view_client', array(
  278. 'jitm_id' => $envelope->id,
  279. ) );
  280. $normalized_site_url = Jetpack::build_raw_urls( get_home_url() );
  281. $envelope->url = 'https://jetpack.com/redirect/?source=jitm-' . $envelope->id . '&site=' . $normalized_site_url . '&u=' . $user->ID;
  282. $envelope->jitm_stats_url = Jetpack::build_stats_url( array( 'x_jetpack-jitm' => $envelope->id ) );
  283. if ( $envelope->CTA->hook ) {
  284. $envelope->url = apply_filters( 'jitm_' . $envelope->CTA->hook, $envelope->url );
  285. unset( $envelope->CTA->hook );
  286. }
  287. if ( isset( $envelope->content->hook ) ) {
  288. $envelope->content = apply_filters( 'jitm_' . $envelope->content->hook, $envelope->content );
  289. unset( $envelope->content->hook );
  290. }
  291. // no point in showing an empty message
  292. if ( empty( $envelope->content->message ) ) {
  293. unset( $envelopes[ $idx ] );
  294. continue;
  295. }
  296. switch ( $envelope->content->icon ) {
  297. case 'jetpack':
  298. $envelope->content->icon = '<div class="jp-emblem">' . Jetpack::get_jp_emblem() . '</div>';
  299. break;
  300. case 'woocommerce':
  301. $envelope->content->icon = '<div class="jp-emblem"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0px" y="0px" viewBox="0 0 168 100" xml:space="preserve" enable-background="new 0 0 168 100" width="50" height="30"><style type="text/css">
  302. .st0{clip-path:url(#SVGID_2_);enable-background:new ;}
  303. .st1{clip-path:url(#SVGID_4_);}
  304. .st2{clip-path:url(#SVGID_6_);}
  305. .st3{clip-path:url(#SVGID_8_);fill:#8F567F;}
  306. .st4{clip-path:url(#SVGID_10_);fill:#FFFFFE;}
  307. .st5{clip-path:url(#SVGID_12_);fill:#FFFFFE;}
  308. .st6{clip-path:url(#SVGID_14_);fill:#FFFFFE;}
  309. </style><g><defs><polygon id="SVGID_1_" points="83.8 100 0 100 0 0.3 83.8 0.3 167.6 0.3 167.6 100 "/></defs><clipPath id="SVGID_2_"><use xlink:href="#SVGID_1_" overflow="visible"/></clipPath><g class="st0"><g><defs><rect id="SVGID_3_" width="168" height="100"/></defs><clipPath id="SVGID_4_"><use xlink:href="#SVGID_3_" overflow="visible"/></clipPath><g class="st1"><defs><path id="SVGID_5_" d="M15.6 0.3H152c8.6 0 15.6 7 15.6 15.6v52c0 8.6-7 15.6-15.6 15.6h-48.9l6.7 16.4L80.2 83.6H15.6C7 83.6 0 76.6 0 67.9v-52C0 7.3 7 0.3 15.6 0.3"/></defs><clipPath id="SVGID_6_"><use xlink:href="#SVGID_5_" overflow="visible"/></clipPath><g class="st2"><defs><rect id="SVGID_7_" width="168" height="100"/></defs><clipPath id="SVGID_8_"><use xlink:href="#SVGID_7_" overflow="visible"/></clipPath><rect x="-10" y="-9.7" class="st3" width="187.6" height="119.7"/></g></g></g></g></g><g><defs><path id="SVGID_9_" d="M8.4 14.5c1-1.3 2.4-2 4.3-2.1 3.5-0.2 5.5 1.4 6 4.9 2.1 14.3 4.4 26.4 6.9 36.4l15-28.6c1.4-2.6 3.1-3.9 5.2-4.1 3-0.2 4.9 1.7 5.6 5.7 1.7 9.1 3.9 16.9 6.5 23.4 1.8-17.4 4.8-30 9-37.7 1-1.9 2.5-2.9 4.5-3 1.6-0.1 3 0.3 4.3 1.4 1.3 1 2 2.3 2.1 3.9 0.1 1.2-0.1 2.3-0.7 3.3 -2.7 5-4.9 13.2-6.6 24.7 -1.7 11.1-2.3 19.8-1.9 26.1 0.1 1.7-0.1 3.2-0.8 4.5 -0.8 1.5-2 2.4-3.7 2.5 -1.8 0.1-3.6-0.7-5.4-2.5C52.4 66.7 47.4 57 43.7 44.1c-4.4 8.8-7.7 15.3-9.9 19.7 -4 7.7-7.5 11.7-10.3 11.9 -1.9 0.1-3.5-1.4-4.8-4.7 -3.5-9-7.3-26.3-11.3-52C7.1 17.3 7.5 15.8 8.4 14.5"/></defs><clipPath id="SVGID_10_"><use xlink:href="#SVGID_9_" overflow="visible"/></clipPath><rect x="-2.7" y="-0.6" class="st4" width="90.6" height="86.4"/></g><g><defs><path id="SVGID_11_" d="M155.6 25.2c-2.5-4.3-6.1-6.9-11-7.9 -1.3-0.3-2.5-0.4-3.7-0.4 -6.6 0-11.9 3.4-16.1 10.2 -3.6 5.8-5.3 12.3-5.3 19.3 0 5.3 1.1 9.8 3.3 13.6 2.5 4.3 6.1 6.9 11 7.9 1.3 0.3 2.5 0.4 3.7 0.4 6.6 0 12-3.4 16.1-10.2 3.6-5.9 5.3-12.4 5.3-19.4C159 33.4 157.9 28.9 155.6 25.2zM147 44.2c-0.9 4.5-2.7 7.9-5.2 10.1 -2 1.8-3.9 2.5-5.5 2.2 -1.7-0.3-3-1.8-4-4.4 -0.8-2.1-1.2-4.2-1.2-6.2 0-1.7 0.2-3.4 0.5-5 0.6-2.8 1.8-5.5 3.6-8.1 2.3-3.3 4.7-4.8 7.1-4.2 1.7 0.3 3 1.8 4 4.4 0.8 2.1 1.2 4.2 1.2 6.2C147.5 40.9 147.3 42.6 147 44.2z"/></defs><clipPath id="SVGID_12_"><use xlink:href="#SVGID_11_" overflow="visible"/></clipPath><rect x="109.6" y="6.9" class="st5" width="59.4" height="71.4"/></g><g><defs><path id="SVGID_13_" d="M112.7 25.2c-2.5-4.3-6.1-6.9-11-7.9 -1.3-0.3-2.5-0.4-3.7-0.4 -6.6 0-11.9 3.4-16.1 10.2 -3.5 5.8-5.3 12.3-5.3 19.3 0 5.3 1.1 9.8 3.3 13.6 2.5 4.3 6.1 6.9 11 7.9 1.3 0.3 2.5 0.4 3.7 0.4 6.6 0 12-3.4 16.1-10.2 3.5-5.9 5.3-12.4 5.3-19.4C116 33.4 114.9 28.9 112.7 25.2zM104.1 44.2c-0.9 4.5-2.7 7.9-5.2 10.1 -2 1.8-3.9 2.5-5.5 2.2 -1.7-0.3-3-1.8-4-4.4 -0.8-2.1-1.2-4.2-1.2-6.2 0-1.7 0.2-3.4 0.5-5 0.6-2.8 1.8-5.5 3.6-8.1 2.3-3.3 4.7-4.8 7.1-4.2 1.7 0.3 3 1.8 4 4.4 0.8 2.1 1.2 4.2 1.2 6.2C104.6 40.9 104.4 42.6 104.1 44.2z"/></defs><clipPath id="SVGID_14_"><use xlink:href="#SVGID_13_" overflow="visible"/></clipPath><rect x="66.7" y="6.9" class="st6" width="59.4" height="71.4"/></g></svg></div>';
  310. break;
  311. default:
  312. $envelope->content->icon = '';
  313. break;
  314. }
  315. $jetpack = Jetpack::init();
  316. $jetpack->stat( 'jitm', $envelope->id . '-viewed-' . JETPACK__VERSION );
  317. $jetpack->do_stats( 'server_side' );
  318. }
  319. return $envelopes;
  320. }
  321. }
  322. add_action( 'init', array( 'Jetpack_JITM', 'init' ) );