| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class WPSEO_HelpScout |
| 10 |
*/ |
| 11 |
class WPSEO_HelpScout implements WPSEO_WordPress_Integration { |
| 12 |
|
| 13 |
/** |
| 14 |
* The id for the beacon. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
protected $beacon_id; |
| 19 |
|
| 20 |
/** |
| 21 |
* The pages where the beacon is loaded. |
| 22 |
* |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
protected $pages; |
| 26 |
|
| 27 |
/** |
| 28 |
* The products the beacon is loaded for. |
| 29 |
* |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
protected $products; |
| 33 |
|
| 34 |
/** |
| 35 |
* Whether to asks the user's consent before loading in HelpScout. |
| 36 |
* |
| 37 |
* @var bool |
| 38 |
*/ |
| 39 |
protected $ask_consent; |
| 40 |
|
| 41 |
/** |
| 42 |
* WPSEO_HelpScout constructor. |
| 43 |
* |
| 44 |
* @param string $beacon_id The beacon id. |
| 45 |
* @param array $pages The pages where the beacon is loaded. |
| 46 |
* @param array $products The products the beacon is loaded for. |
| 47 |
* @param bool $ask_consent Optional. Whether to ask for consent before loading in HelpScout. |
| 48 |
*/ |
| 49 |
public function __construct( $beacon_id, array $pages, array $products, $ask_consent = false ) { |
| 50 |
$this->beacon_id = $beacon_id; |
| 51 |
$this->pages = $pages; |
| 52 |
$this->products = $products; |
| 53 |
$this->ask_consent = $ask_consent; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* {@inheritDoc} |
| 58 |
*/ |
| 59 |
public function register_hooks() { |
| 60 |
if ( ! $this->is_beacon_page() ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_help_scout_script' ] ); |
| 65 |
add_action( 'admin_footer', [ $this, 'output_beacon_js' ] ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Enqueues the HelpScout script. |
| 70 |
*/ |
| 71 |
public function enqueue_help_scout_script() { |
| 72 |
$asset_manager = new WPSEO_Admin_Asset_Manager(); |
| 73 |
$asset_manager->enqueue_script( 'help-scout-beacon' ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Outputs a small piece of javascript for the beacon. |
| 78 |
*/ |
| 79 |
public function output_beacon_js() { |
| 80 |
printf( |
| 81 |
'<script type="text/javascript">window.%1$s(\'%2$s\', %3$s)</script>', |
| 82 |
( $this->ask_consent ) ? 'wpseoHelpScoutBeaconConsent' : 'wpseoHelpScoutBeacon', |
| 83 |
esc_html( $this->beacon_id ), |
| 84 |
WPSEO_Utils::format_json_encode( $this->get_session_data() ) |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Checks if the current page is a page containing the beacon. |
| 90 |
* |
| 91 |
* @return bool |
| 92 |
*/ |
| 93 |
private function is_beacon_page() { |
| 94 |
return in_array( $this->get_current_page(), $this->pages, true ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Retrieves the value of the current page. |
| 99 |
* |
| 100 |
* @return string The current page. |
| 101 |
*/ |
| 102 |
private function get_current_page() { |
| 103 |
$page = filter_input( INPUT_GET, 'page' ); |
| 104 |
if ( isset( $page ) && $page !== false ) { |
| 105 |
return $page; |
| 106 |
} |
| 107 |
|
| 108 |
return ''; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Retrieves the identifying data. |
| 113 |
* |
| 114 |
* @return string The data to pass as identifying data. |
| 115 |
*/ |
| 116 |
protected function get_session_data() { |
| 117 |
/** @noinspection PhpUnusedLocalVariableInspection */ |
| 118 |
// Do not make these strings translatable! They are for our support agents, the user won't see them! |
| 119 |
$current_user = wp_get_current_user(); |
| 120 |
|
| 121 |
$data = [ |
| 122 |
'name' => trim( $current_user->user_firstname . ' ' . $current_user->user_lastname ), |
| 123 |
'email' => $current_user->user_email, |
| 124 |
'WordPress Version' => $this->get_wordpress_version(), |
| 125 |
'Server' => $this->get_server_info(), |
| 126 |
'<a href="' . admin_url( 'themes.php' ) . '">Theme</a>' => $this->get_theme_info(), |
| 127 |
'<a href="' . admin_url( 'plugins.php' ) . '">Plugins</a>' => $this->get_active_plugins(), |
| 128 |
]; |
| 129 |
|
| 130 |
if ( ! empty( $this->products ) ) { |
| 131 |
$addon_manager = new WPSEO_Addon_Manager(); |
| 132 |
foreach ( $this->products as $product ) { |
| 133 |
$subscription = $addon_manager->get_subscription( $product ); |
| 134 |
|
| 135 |
if ( ! $subscription ) { |
| 136 |
continue; |
| 137 |
} |
| 138 |
|
| 139 |
$data[ $subscription->product->name ] = $this->get_product_info( $subscription ); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return WPSEO_Utils::format_json_encode( $data ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Returns basic info about the server software. |
| 148 |
* |
| 149 |
* @return string |
| 150 |
*/ |
| 151 |
private function get_server_info() { |
| 152 |
$server_tracking_data = new WPSEO_Tracking_Server_Data(); |
| 153 |
$server_data = $server_tracking_data->get(); |
| 154 |
$server_data = $server_data['server']; |
| 155 |
|
| 156 |
$fields_to_use = [ |
| 157 |
'IP' => 'ip', |
| 158 |
'Hostname' => 'Hostname', |
| 159 |
'OS' => 'os', |
| 160 |
'PHP' => 'PhpVersion', |
| 161 |
'CURL' => 'CurlVersion', |
| 162 |
]; |
| 163 |
|
| 164 |
$server_data['CurlVersion'] = $server_data['CurlVersion']['version'] . '(SSL Support' . $server_data['CurlVersion']['sslSupport'] . ')'; |
| 165 |
|
| 166 |
$server_info = '<table>'; |
| 167 |
|
| 168 |
foreach ( $fields_to_use as $label => $field_to_use ) { |
| 169 |
if ( isset( $server_data[ $field_to_use ] ) ) { |
| 170 |
$server_info .= sprintf( '<tr><td>%1$s</td><td>%2$s</td></tr>', esc_html( $label ), esc_html( $server_data[ $field_to_use ] ) ); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
$server_info .= '</table>'; |
| 175 |
|
| 176 |
return $server_info; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Returns info about the Yoast SEO plugin version and license. |
| 181 |
* |
| 182 |
* @param object $plugin The plugin. |
| 183 |
* |
| 184 |
* @return string The product info. |
| 185 |
*/ |
| 186 |
private function get_product_info( $plugin ) { |
| 187 |
if ( empty( $plugin ) ) { |
| 188 |
return ''; |
| 189 |
} |
| 190 |
|
| 191 |
$product_info = '<table>'; |
| 192 |
$product_info .= '<tr><td>Version</td><td>' . $plugin->product->version . '</td></tr>'; |
| 193 |
$product_info .= '<tr><td>Expiration date</td><td>' . $plugin->expiry_date . '</td></tr>'; |
| 194 |
$product_info .= '</table>'; |
| 195 |
|
| 196 |
return $product_info; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Returns the WordPress version + a suffix if current WP is multi site. |
| 201 |
* |
| 202 |
* @return string The WordPress version string. |
| 203 |
*/ |
| 204 |
private function get_wordpress_version() { |
| 205 |
global $wp_version; |
| 206 |
|
| 207 |
$wordpress_version = $wp_version; |
| 208 |
if ( is_multisite() ) { |
| 209 |
$wordpress_version .= ' MULTI-SITE'; |
| 210 |
} |
| 211 |
|
| 212 |
return $wordpress_version; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Returns a formatted HTML string for the current theme. |
| 217 |
* |
| 218 |
* @return string The theme info as string. |
| 219 |
*/ |
| 220 |
private function get_theme_info() { |
| 221 |
$theme = wp_get_theme(); |
| 222 |
|
| 223 |
$theme_info = sprintf( |
| 224 |
'<a href="%1$s">%2$s</a> v%3$s by %4$s', |
| 225 |
esc_attr( $theme->display( 'ThemeURI' ) ), |
| 226 |
esc_html( $theme->display( 'Name' ) ), |
| 227 |
esc_html( $theme->display( 'Version' ) ), |
| 228 |
esc_html( $theme->display( 'Author' ) ) |
| 229 |
); |
| 230 |
|
| 231 |
if ( is_child_theme() ) { |
| 232 |
$theme_info .= sprintf( '<br />Child theme of: %1$s', esc_html( $theme->display( 'Template' ) ) ); |
| 233 |
} |
| 234 |
|
| 235 |
return $theme_info; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Returns a formatted HTML list of all active plugins. |
| 240 |
* |
| 241 |
* @return string The active plugins. |
| 242 |
*/ |
| 243 |
private function get_active_plugins() { |
| 244 |
$updates_available = get_site_transient( 'update_plugins' ); |
| 245 |
|
| 246 |
$active_plugins = ''; |
| 247 |
foreach ( wp_get_active_and_valid_plugins() as $plugin ) { |
| 248 |
$plugin_data = get_plugin_data( $plugin ); |
| 249 |
$plugin_file = str_replace( trailingslashit( WP_PLUGIN_DIR ), '', $plugin ); |
| 250 |
|
| 251 |
if ( isset( $updates_available->response[ $plugin_file ] ) ) { |
| 252 |
$active_plugins .= '<i class="icon-close1"></i> '; |
| 253 |
} |
| 254 |
|
| 255 |
$active_plugins .= sprintf( |
| 256 |
'<a href="%1$s">%2$s</a> v%3$s', |
| 257 |
esc_attr( $plugin_data['PluginURI'] ), |
| 258 |
esc_html( $plugin_data['Name'] ), |
| 259 |
esc_html( $plugin_data['Version'] ) |
| 260 |
); |
| 261 |
} |
| 262 |
|
| 263 |
return $active_plugins; |
| 264 |
} |
| 265 |
} |
| 266 |
|