PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/server/wordpress/wp-content/plugins/wordpress-seo/src/integrations/admin/helpscout-beacon.php

https://gitlab.com/suporte.spturis/carnaval2015.spturis.com.br
PHP | 351 lines | 176 code | 53 blank | 122 comment | 14 complexity | 8d35e13e2bfe71670b853b6c4a69a6a5 MD5 | raw file
  1. <?php
  2. namespace Yoast\WP\SEO\Integrations\Admin;
  3. use WPSEO_Addon_Manager;
  4. use WPSEO_Admin_Asset_Manager;
  5. use WPSEO_Tracking_Server_Data;
  6. use WPSEO_Utils;
  7. use Yoast\WP\SEO\Conditionals\Admin_Conditional;
  8. use Yoast\WP\SEO\Helpers\Options_Helper;
  9. use Yoast\WP\SEO\Integrations\Integration_Interface;
  10. /**
  11. * Class WPSEO_HelpScout
  12. */
  13. class HelpScout_Beacon implements Integration_Interface {
  14. /**
  15. * The id for the beacon.
  16. *
  17. * @var string
  18. */
  19. protected $beacon_id = '2496aba6-0292-489c-8f5d-1c0fba417c2f';
  20. /**
  21. * The id for the beacon for users that have tracking on.
  22. *
  23. * @var string
  24. */
  25. protected $beacon_id_tracking_users = '6b8e74c5-aa81-4295-b97b-c2a62a13ea7f';
  26. /**
  27. * The products the beacon is loaded for.
  28. *
  29. * @var array
  30. */
  31. protected $products = [];
  32. /**
  33. * Whether to asks the user's consent before loading in HelpScout.
  34. *
  35. * @var bool
  36. */
  37. protected $ask_consent = true;
  38. /**
  39. * The options helper.
  40. *
  41. * @var Options_Helper
  42. */
  43. protected $options;
  44. /**
  45. * The array of pages we need to show the beacon on with their respective beacon IDs.
  46. *
  47. * @var array
  48. */
  49. protected $pages_ids;
  50. /**
  51. * The array of pages we need to show the beacon on.
  52. *
  53. * @var array
  54. */
  55. protected $base_pages = [
  56. 'wpseo_dashboard',
  57. 'wpseo_titles',
  58. 'wpseo_search_console',
  59. 'wpseo_social',
  60. 'wpseo_tools',
  61. 'wpseo_licenses',
  62. 'wpseo_workouts',
  63. ];
  64. /**
  65. * The current admin page
  66. *
  67. * @var string
  68. */
  69. protected $page;
  70. /**
  71. * The asset manager.
  72. *
  73. * @var WPSEO_Admin_Asset_Manager
  74. */
  75. protected $asset_manager;
  76. /**
  77. * Headless_Rest_Endpoints_Enabled_Conditional constructor.
  78. *
  79. * @param Options_Helper $options The options helper.
  80. * @param WPSEO_Admin_Asset_Manager $asset_manager The asset manager.
  81. */
  82. public function __construct( Options_Helper $options, WPSEO_Admin_Asset_Manager $asset_manager ) {
  83. $this->options = $options;
  84. $this->asset_manager = $asset_manager;
  85. $this->ask_consent = ! $this->options->get( 'tracking' );
  86. $this->page = \filter_input( \INPUT_GET, 'page', \FILTER_SANITIZE_STRING );
  87. foreach ( $this->base_pages as $page ) {
  88. if ( $this->ask_consent ) {
  89. // We want to be able to show surveys to people who have tracking on, so we give them a different beacon.
  90. $this->pages_ids[ $page ] = $this->beacon_id_tracking_users;
  91. }
  92. else {
  93. $this->pages_ids[ $page ] = $this->beacon_id;
  94. }
  95. }
  96. }
  97. /**
  98. * {@inheritDoc}
  99. */
  100. public function register_hooks() {
  101. \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_help_scout_script' ] );
  102. \add_action( 'admin_footer', [ $this, 'output_beacon_js' ] );
  103. }
  104. /**
  105. * Enqueues the HelpScout script.
  106. */
  107. public function enqueue_help_scout_script() {
  108. // Make sure plugins can filter in their "stuff", before we check whether we're outputting a beacon.
  109. $this->filter_settings();
  110. if ( ! $this->is_beacon_page() ) {
  111. return;
  112. }
  113. $this->asset_manager->enqueue_script( 'help-scout-beacon' );
  114. }
  115. /**
  116. * Outputs a small piece of javascript for the beacon.
  117. */
  118. public function output_beacon_js() {
  119. if ( ! $this->is_beacon_page() ) {
  120. return;
  121. }
  122. \printf(
  123. '<script type="text/javascript">window.%1$s(\'%2$s\', %3$s)</script>',
  124. ( $this->ask_consent ) ? 'wpseoHelpScoutBeaconConsent' : 'wpseoHelpScoutBeacon',
  125. \esc_html( $this->pages_ids[ $this->page ] ),
  126. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaping done in format_json_encode.
  127. WPSEO_Utils::format_json_encode( (array) $this->get_session_data() )
  128. );
  129. }
  130. /**
  131. * Checks if the current page is a page containing the beacon.
  132. *
  133. * @return bool
  134. */
  135. private function is_beacon_page() {
  136. $return = false;
  137. if ( ! empty( $this->page ) && $GLOBALS['pagenow'] === 'admin.php' && isset( $this->pages_ids[ $this->page ] ) ) {
  138. $return = true;
  139. }
  140. /**
  141. * Filter: 'wpseo_helpscout_show_beacon' - Allows overriding whether we show the HelpScout beacon.
  142. *
  143. * @api bool - Whether we show the beacon or not.
  144. */
  145. return \apply_filters( 'wpseo_helpscout_show_beacon', $return );
  146. }
  147. /**
  148. * Retrieves the identifying data.
  149. *
  150. * @return string The data to pass as identifying data.
  151. */
  152. protected function get_session_data() {
  153. $current_user = \wp_get_current_user();
  154. // Do not make these strings translatable! They are for our support agents, the user won't see them!
  155. $data = [
  156. 'name' => \trim( $current_user->user_firstname . ' ' . $current_user->user_lastname ),
  157. 'email' => $current_user->user_email,
  158. 'WordPress Version' => $this->get_wordpress_version(),
  159. 'Server' => $this->get_server_info(),
  160. '<a href="' . \admin_url( 'themes.php' ) . '">Theme</a>' => $this->get_theme_info(),
  161. '<a href="' . \admin_url( 'plugins.php' ) . '">Plugins</a>' => $this->get_active_plugins(),
  162. ];
  163. if ( ! empty( $this->products ) ) {
  164. $addon_manager = new WPSEO_Addon_Manager();
  165. foreach ( $this->products as $product ) {
  166. $subscription = $addon_manager->get_subscription( $product );
  167. if ( ! $subscription ) {
  168. continue;
  169. }
  170. $data[ $subscription->product->name ] = $this->get_product_info( $subscription );
  171. }
  172. }
  173. return WPSEO_Utils::format_json_encode( $data );
  174. }
  175. /**
  176. * Returns basic info about the server software.
  177. *
  178. * @return string
  179. */
  180. private function get_server_info() {
  181. $server_tracking_data = new WPSEO_Tracking_Server_Data();
  182. $server_data = $server_tracking_data->get();
  183. $server_data = $server_data['server'];
  184. $fields_to_use = [
  185. 'IP' => 'ip',
  186. 'Hostname' => 'Hostname',
  187. 'OS' => 'os',
  188. 'PHP' => 'PhpVersion',
  189. 'CURL' => 'CurlVersion',
  190. ];
  191. $server_data['CurlVersion'] = $server_data['CurlVersion']['version'] . '(SSL Support' . $server_data['CurlVersion']['sslSupport'] . ')';
  192. $server_info = '<table>';
  193. foreach ( $fields_to_use as $label => $field_to_use ) {
  194. if ( isset( $server_data[ $field_to_use ] ) ) {
  195. $server_info .= \sprintf( '<tr><td>%1$s</td><td>%2$s</td></tr>', \esc_html( $label ), \esc_html( $server_data[ $field_to_use ] ) );
  196. }
  197. }
  198. $server_info .= '</table>';
  199. return $server_info;
  200. }
  201. /**
  202. * Returns info about the Yoast SEO plugin version and license.
  203. *
  204. * @param object $plugin The plugin.
  205. *
  206. * @return string The product info.
  207. */
  208. private function get_product_info( $plugin ) {
  209. if ( empty( $plugin ) ) {
  210. return '';
  211. }
  212. $product_info = '<table>';
  213. $product_info .= '<tr><td>Version</td><td>' . $plugin->product->version . '</td></tr>';
  214. $product_info .= '<tr><td>Expiration date</td><td>' . $plugin->expiry_date . '</td></tr>';
  215. $product_info .= '</table>';
  216. return $product_info;
  217. }
  218. /**
  219. * Returns the WordPress version + a suffix if current WP is multi site.
  220. *
  221. * @return string The WordPress version string.
  222. */
  223. private function get_wordpress_version() {
  224. global $wp_version;
  225. $wordpress_version = $wp_version;
  226. if ( \is_multisite() ) {
  227. $wordpress_version .= ' MULTI-SITE';
  228. }
  229. return $wordpress_version;
  230. }
  231. /**
  232. * Returns a formatted HTML string for the current theme.
  233. *
  234. * @return string The theme info as string.
  235. */
  236. private function get_theme_info() {
  237. $theme = \wp_get_theme();
  238. $theme_info = \sprintf(
  239. '<a href="%1$s">%2$s</a> v%3$s by %4$s',
  240. \esc_attr( $theme->display( 'ThemeURI' ) ),
  241. \esc_html( $theme->display( 'Name' ) ),
  242. \esc_html( $theme->display( 'Version' ) ),
  243. \esc_html( $theme->display( 'Author' ) )
  244. );
  245. if ( \is_child_theme() ) {
  246. $theme_info .= \sprintf( '<br />Child theme of: %1$s', \esc_html( $theme->display( 'Template' ) ) );
  247. }
  248. return $theme_info;
  249. }
  250. /**
  251. * Returns a formatted HTML list of all active plugins.
  252. *
  253. * @return string The active plugins.
  254. */
  255. private function get_active_plugins() {
  256. $updates_available = \get_site_transient( 'update_plugins' );
  257. $active_plugins = '';
  258. foreach ( \wp_get_active_and_valid_plugins() as $plugin ) {
  259. $plugin_data = \get_plugin_data( $plugin );
  260. $plugin_file = \str_replace( \trailingslashit( \WP_PLUGIN_DIR ), '', $plugin );
  261. if ( isset( $updates_available->response[ $plugin_file ] ) ) {
  262. $active_plugins .= '<i class="icon-close1"></i> ';
  263. }
  264. $active_plugins .= \sprintf(
  265. '<a href="%1$s">%2$s</a> v%3$s',
  266. \esc_attr( $plugin_data['PluginURI'] ),
  267. \esc_html( $plugin_data['Name'] ),
  268. \esc_html( $plugin_data['Version'] )
  269. );
  270. }
  271. return $active_plugins;
  272. }
  273. /**
  274. * Returns the conditionals based on which this integration should be active.
  275. *
  276. * @return array The array of conditionals.
  277. */
  278. public static function get_conditionals() {
  279. return [ Admin_Conditional::class ];
  280. }
  281. /**
  282. * Allows filtering of the HelpScout settings. Hooked to admin_head to prevent timing issues, not too early, not too late.
  283. */
  284. protected function filter_settings() {
  285. /**
  286. * Filter: 'wpseo_helpscout_beacon_settings' - Allows overriding the HelpScout beacon settings.
  287. *
  288. * @api string - The HelpScout beacon settings.
  289. */
  290. $filterable_helpscout_setting = [
  291. 'products' => $this->products,
  292. 'pages_ids' => $this->pages_ids,
  293. ];
  294. $helpscout_settings = \apply_filters( 'wpseo_helpscout_beacon_settings', $filterable_helpscout_setting );
  295. $this->products = $helpscout_settings['products'];
  296. $this->pages_ids = $helpscout_settings['pages_ids'];
  297. }
  298. }