PageRenderTime 34ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/class-pointers.php

https://gitlab.com/Blueprint-Marketing/google-analytics-for-wordpress
PHP | 274 lines | 155 code | 35 blank | 84 comment | 8 complexity | 66fa030fdacff01af3534b03d63f4648 MD5 | raw file
  1. <?php
  2. /**
  3. * @package WPSEO\Admin
  4. */
  5. /**
  6. * This class handles the pointers used in the introduction tour.
  7. */
  8. class Yoast_GA_Pointers {
  9. /**
  10. * @var Yoast_GA_Pointers
  11. */
  12. public static $instance;
  13. /**
  14. * @var array Holds the buttons to be put out
  15. */
  16. private $button_array = array();
  17. /**
  18. * @var array Holds the options such as content and position.
  19. */
  20. private $options_array = array();
  21. /**
  22. * @var string Holds the current styling selector.
  23. */
  24. private $selector;
  25. /**
  26. * @var array Holds the admin pages we have pointers for and the callback that generates the pointers content
  27. */
  28. private $admin_pages = array(
  29. 'yst_ga_settings' => 'settings_pointer',
  30. 'yst_ga_dashboard' => 'dashboard_pointer',
  31. 'yst_ga_extensions' => 'extensions_pointer',
  32. );
  33. /**
  34. * Class constructor.
  35. */
  36. public function __construct() {
  37. if ( current_user_can( 'manage_options' ) && ! get_user_meta( get_current_user_id(), 'ga_ignore_tour' ) ) {
  38. Yoast_GA_Admin_Assets::load_tour_assets( $this->localize_script() );
  39. }
  40. }
  41. /**
  42. * Determine whether to call prepare_page_pointer or prepare_tour_pointer and return all needed information in an array.
  43. *
  44. * @return array
  45. */
  46. protected function localize_script() {
  47. $this->prepare_pointer();
  48. $button_array_defaults = array(
  49. 'primary_button' => array(
  50. 'text' => false,
  51. 'function' => '',
  52. ),
  53. 'previous_button' => array(
  54. 'text' => false,
  55. 'function' => '',
  56. ),
  57. );
  58. $this->button_array = wp_parse_args( $this->button_array, $button_array_defaults );
  59. return array(
  60. 'selector' => $this->selector,
  61. 'ignore_url' => $this->get_ignore_url(),
  62. 'options' => $this->options_array,
  63. 'buttons' => $this->button_array,
  64. 'close_button_text' => __( 'Close', 'google-analytics-for-wordpress' ),
  65. );
  66. }
  67. /**
  68. * Check if we need to run the tour or the page pointer.
  69. */
  70. protected function prepare_pointer() {
  71. global $pagenow;
  72. $page = $this->get_current_page();
  73. if ( $pagenow === 'admin.php' && array_key_exists( $page, $this->admin_pages ) ) {
  74. $this->prepare_page_pointer( $page );
  75. }
  76. else {
  77. $this->prepare_tour_pointer();
  78. }
  79. }
  80. /**
  81. * Get the singleton instance of this class
  82. *
  83. * @return Yoast_GA_Pointers
  84. */
  85. public static function get_instance() {
  86. if ( ! ( self::$instance instanceof self ) ) {
  87. self::$instance = new self();
  88. }
  89. return self::$instance;
  90. }
  91. /**
  92. * Get the current page the user is on.
  93. *
  94. * @return string
  95. */
  96. protected function get_current_page() {
  97. return filter_input( INPUT_GET, 'page' );
  98. }
  99. /**
  100. * Shows a pointer on the proper pages
  101. *
  102. * @param string $page
  103. */
  104. protected function prepare_page_pointer( $page ) {
  105. $method = $this->admin_pages[ $page ];
  106. $pointer = $this->$method();
  107. $this->selector = '#yoast_ga_title';
  108. $this->options_array = array(
  109. 'content' => $pointer['content'],
  110. 'position' => array(
  111. 'edge' => 'top',
  112. 'align' => ( is_rtl() ) ? 'left' : 'right',
  113. ),
  114. 'pointerWidth' => 450,
  115. );
  116. if ( isset( $pointer['next_page'] ) ) {
  117. $this->add_button( 'primary_button', __( 'Next', 'google-analytics-for-wordpress' ), admin_url( 'admin.php?page=yst_ga_' . $pointer['next_page'] ) );
  118. }
  119. if ( isset( $pointer['prev_page'] ) ) {
  120. $this->add_button( 'previous_button', __( 'Previous', 'google-analytics-for-wordpress' ), admin_url( 'admin.php?page=yst_ga_' . $pointer['prev_page'] ) );
  121. }
  122. }
  123. /**
  124. * Create the button to navigate the tour.
  125. *
  126. * @param string $key
  127. * @param string $text
  128. * @param string $location
  129. *
  130. * @return array
  131. */
  132. protected function add_button( $key, $text, $location ) {
  133. return $this->button_array[ $key ] = array(
  134. 'text' => $text,
  135. 'location' => $location,
  136. );
  137. }
  138. /**
  139. * Show a pointer that starts the tour for WordPress SEO
  140. */
  141. protected function prepare_tour_pointer() {
  142. $content = '<h3>' . __( 'Congratulations!', 'google-analytics-for-wordpress' ) . '</h3>'
  143. . '<p>' . __( 'You\'ve just installed Google Analytics by Yoast! Click "Start tour" to view a quick introduction of this plugin\'s core functionality.', 'google-analytics-for-wordpress' ) . '</p>';
  144. $this->selector = 'li#toplevel_page_yst_ga_dashboard';
  145. $this->options_array = array(
  146. 'content' => $content,
  147. 'position' => array(
  148. 'edge' => 'top',
  149. 'align' => 'center',
  150. ),
  151. );
  152. $this->add_button( 'primary_button', __( 'Start tour', 'google-analytics-for-wordpress' ), admin_url( 'admin.php?page=yst_ga_settings' ) );
  153. }
  154. /**
  155. * Extending the current page URL with two params to be able to ignore the tour.
  156. *
  157. * @return mixed
  158. */
  159. protected function get_ignore_url() {
  160. $ignore_tour_parameters = array(
  161. 'ga_restart_tour' => false,
  162. 'ga_ignore_tour' => '1',
  163. 'nonce' => wp_create_nonce( 'ga-ignore-tour' ),
  164. );
  165. return esc_url( add_query_arg( $ignore_tour_parameters ) );
  166. }
  167. /**
  168. * Returns the content of the settings pointer
  169. *
  170. * @return array
  171. */
  172. private function settings_pointer() {
  173. global $current_user;
  174. return array(
  175. 'content' => '<h3>' . __( 'Settings', 'google-analytics-for-wordpress' ) . '</h3>'
  176. . '<p><strong>' . __( 'Tab: General', 'google-analytics-for-wordpress' ) . '</strong></p>'
  177. /* translators: %s is the product name 'Google Analytics by Yoast' */
  178. . '<p>' . sprintf( __( 'These are the general settings for %s. Here you can authenticate and connect your Google Analytics profile, enable general tracking features and restart this tour.', 'google-analytics-for-wordpress' ), 'Google Analytics by Yoast' ) . '</p>'
  179. . '<p><strong>' . __( 'Tab: Universal', 'google-analytics-for-wordpress' ) . '</strong></p>'
  180. . '<p>' . __( 'Enable Universal tracking and tracking features related to Universal tracking.', 'google-analytics-for-wordpress' ) . '</p>'
  181. . '<p><strong>' . __( 'Tab: Advanced', 'google-analytics-for-wordpress' ) . '</strong></p>'
  182. . '<p>' . __( 'The section where you can find the advanced settings of this plugin. Here you can alter how you track certain things and add custom code if necessary. Only use this if you know what you’re doing.', 'google-analytics-for-wordpress' ) . '</p>'
  183. . '<p><strong>' . __( 'Tab: Custom dimensions', 'google-analytics-for-wordpress' ) . '</strong></p>'
  184. /* translators: %s links to `https://yoast.com/wordpress/plugins/google-analytics/` */
  185. . '<p>' . sprintf( __( 'You can only use this functionality if you have %s. Custom dimensions allow for much more powerful and specific tracking.', 'google-analytics-for-wordpress' ), '<a href="https://yoast.com/wordpress/plugins/google-analytics/#utm_source=ga_settings&utm_medium=ga_tour&utm_campaign=tour">Google Analytics by Yoast Premium</a>' ) . '</p>'
  186. . '<p><strong>' . __( 'Tab: Debug mode', 'google-analytics-for-wordpress' ) . '</strong></p>'
  187. . '<p>' . __( 'Only use this if you know what you’re doing. Here you can check what could be hindering your tracking.', 'google-analytics-for-wordpress' ) . '</p>'
  188. . '<p><strong style="font-size:150%;">' . __( 'Subscribe to our Newsletter', 'google-analytics-for-wordpress' ) . '</strong><br/>'
  189. . __( 'If you would like us to keep you up-to-date regarding Google Analytics and other plugins by Yoast, subscribe to our newsletter:', 'google-analytics-for-wordpress' ) . '</p>'
  190. . '<form target="_blank" action="http://yoast.us1.list-manage1.com/subscribe/post?u=ffa93edfe21752c921f860358&amp;id=972f1c9122" method="post" selector="newsletter-form" accept-charset="' . esc_attr( get_bloginfo( 'charset' ) ) . '">'
  191. . '<p>'
  192. . '<input style="margin: 5px; color:#666" name="EMAIL" value="' . esc_attr( $current_user->user_email ) . '" selector="newsletter-email" placeholder="' . __( 'Email', 'google-analytics-for-wordpress' ) . '"/>'
  193. . '<input type="hidden" name="group" value="2"/>'
  194. . '<button type="submit" class="button-primary">' . __( 'Subscribe', 'google-analytics-for-wordpress' ) . '</button>'
  195. . '</p>'
  196. . '</form>',
  197. 'next_page' => 'dashboard',
  198. );
  199. }
  200. /**
  201. * Returns the content of the General Settings page pointer
  202. *
  203. * @return array
  204. */
  205. private function dashboard_pointer() {
  206. return array(
  207. 'content' => '<h3>' . __( 'Dashboard', 'google-analytics-for-wordpress' ) . '</h3>'
  208. . '<p><strong>' . __( 'Tab: Overview', 'google-analytics-for-wordpress' ) . '</strong><br/>'
  209. . __( 'View your website’s last month’s analytics, such as sessions and bounce rate.', 'google-analytics-for-wordpress' ) . '</p>'
  210. . '<p><strong>' . __( 'Tab: Reports', 'google-analytics-for-wordpress' ) . '</strong><br/>'
  211. . __( 'View specific reports of your site’s analytics, such as traffic sources, your site’s popular pages and countries where your visitors come from.', 'google-analytics-for-wordpress' ) . '</p>'
  212. . '<p><strong>' . __( 'Tab: Custom dimension reports', 'google-analytics-for-wordpress' )
  213. . '</strong><br/>' . __( 'View basic reports of your custom dimensions, such as traffic per author, per category, etc.', 'google-analytics-for-wordpress' ) . '</p>',
  214. 'next_page' => 'extensions',
  215. 'prev_page' => 'settings',
  216. );
  217. }
  218. /**
  219. * Returns the content of the extensions and licenses page pointer
  220. *
  221. * @return array
  222. */
  223. private function extensions_pointer() {
  224. return array(
  225. 'content' => '<h3>' . __( 'Extensions and Licenses', 'google-analytics-for-wordpress' ) . '</h3>'
  226. . '<p><strong>' . __( 'Tab: Extensions', 'google-analytics-for-wordpress' ) . '</strong><br/>'
  227. /* translators: %s links to `https://yoast.com/wordpress/plugins/`. */
  228. . sprintf( __( 'See which extensions you have installed and which you haven’t installed yet. You can find extensions to our Google Analytics plugin %shere%s.', 'google-analytics-for-wordpress' ), '<a href="https://yoast.com/wordpress/plugins/#utm_source=ga_licenses&utm_medium=ga_tour&utm_campaign=tour">', '</a>' ) . '</p>'
  229. . '<p><strong>' . __( 'Tab: Licenses', 'google-analytics-for-wordpress' ) . '</strong><br/>'
  230. . __( 'Here you can activate, deactivate and renew your licenses.', 'google-analytics-for-wordpress' ) . '</p>'
  231. . '<p><strong>' . __( 'Like this plugin?', 'google-analytics-for-wordpress' ) . '</strong><br/>'
  232. /* translators: %s links to `https://wordpress.org/plugins/google-analytics-for-wordpress/` */
  233. . sprintf( __( 'So, we&#8217;ve come to the end of the tour. If you like the plugin, please %srate it 5 stars on WordPress.org%s!', 'google-analytics-for-wordpress' ), '<a target="_blank" href="https://wordpress.org/plugins/google-analytics-for-wordpress/">', '</a>' ) . '</p>'
  234. /* translators: %s links to `https://yoast.com` */
  235. . '<p>' . sprintf( __( 'Thank you for using our plugin and good luck with your Analytics!<br/><br/>Best,<br/>Team Yoast - %sYoast.com%s', 'google-analytics-for-wordpress' ), '<a target="_blank" href="' . esc_url( 'https://yoast.com/#utm_source=wpseo_licenses&utm_medium=wpseo_tour&utm_campaign=tour' ) . '">', '</a>' ) . '</p>',
  236. 'prev_page' => 'dashboard',
  237. );
  238. }
  239. } /* End of class */