/wp-content/plugins/google-analytics-for-wordpress/assets/lib/pandora/class-am-deactivation-survey.php

https://bitbucket.org/carloskikea/helpet · PHP · 343 lines · 250 code · 21 blank · 72 comment · 24 complexity · 4b9b3014d9945ac6ccdb0f5af6d8c3df MD5 · raw file

  1. <?php
  2. if ( ! class_exists( 'AM_Deactivation_Survey' ) ) {
  3. /**
  4. * Awesome Motive Deactivation Survey.
  5. *
  6. * This prompts the user for more details when they deactivate the plugin.
  7. *
  8. * @version 1.2.1
  9. * @package AwesomeMotive
  10. * @author Jared Atchison and Chris Christoff
  11. * @license GPL-2.0+
  12. * @copyright Copyright (c) 2018
  13. */
  14. class AM_Deactivation_Survey {
  15. /**
  16. * The API URL we are calling.
  17. *
  18. * @since 1.0.0
  19. * @var string
  20. */
  21. public $api_url = 'https://api.awesomemotive.com/v1/deactivation-survey/';
  22. /**
  23. * Name for this plugin.
  24. *
  25. * @since 1.0.0
  26. * @var string
  27. */
  28. public $name;
  29. /**
  30. * Unique slug for this plugin.
  31. *
  32. * @since 1.0.0
  33. * @var string
  34. */
  35. public $plugin;
  36. /**
  37. * Primary class constructor.
  38. *
  39. * @since 1.0.0
  40. * @param string $name Plugin name.
  41. * @param string $plugin Plugin slug.
  42. */
  43. public function __construct( $name = '', $plugin = '' ) {
  44. $this->name = $name;
  45. $this->plugin = $plugin;
  46. // Don't run deactivation survey on dev sites.
  47. if ( $this->is_dev_url() ) {
  48. return;
  49. }
  50. add_action( 'admin_print_scripts', array( $this, 'js' ), 20 );
  51. add_action( 'admin_print_scripts', array( $this, 'css' ) );
  52. add_action( 'admin_footer', array( $this, 'modal' ) );
  53. }
  54. /**
  55. * Checks if current site is a development one.
  56. *
  57. * @since 1.2.0
  58. * @return bool
  59. */
  60. public function is_dev_url() {
  61. // If it is an AM dev site, return false, so we can see them on our dev sites.
  62. if ( defined ('AWESOMEMOTIVE_DEV_MODE' ) && AWESOMEMOTIVE_DEV_MODE ) {
  63. return false;
  64. }
  65. $url = network_site_url( '/' );
  66. $is_local_url = false;
  67. // Trim it up
  68. $url = strtolower( trim( $url ) );
  69. // Need to get the host...so let's add the scheme so we can use parse_url
  70. if ( false === strpos( $url, 'http://' ) && false === strpos( $url, 'https://' ) ) {
  71. $url = 'http://' . $url;
  72. }
  73. $url_parts = parse_url( $url );
  74. $host = ! empty( $url_parts['host'] ) ? $url_parts['host'] : false;
  75. if ( ! empty( $url ) && ! empty( $host ) ) {
  76. if ( false !== ip2long( $host ) ) {
  77. if ( ! filter_var( $host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) ) {
  78. $is_local_url = true;
  79. }
  80. } else if ( 'localhost' === $host ) {
  81. $is_local_url = true;
  82. }
  83. $tlds_to_check = array( '.dev', '.local', ':8888' );
  84. foreach ( $tlds_to_check as $tld ) {
  85. if ( false !== strpos( $host, $tld ) ) {
  86. $is_local_url = true;
  87. continue;
  88. }
  89. }
  90. if ( substr_count( $host, '.' ) > 1 ) {
  91. $subdomains_to_check = array( 'dev.', '*.staging.', 'beta.', 'test.' );
  92. foreach ( $subdomains_to_check as $subdomain ) {
  93. $subdomain = str_replace( '.', '(.)', $subdomain );
  94. $subdomain = str_replace( array( '*', '(.)' ), '(.*)', $subdomain );
  95. if ( preg_match( '/^(' . $subdomain . ')/', $host ) ) {
  96. $is_local_url = true;
  97. continue;
  98. }
  99. }
  100. }
  101. }
  102. return $is_local_url;
  103. }
  104. /**
  105. * Checks if current admin screen is the plugins page.
  106. *
  107. * @since 1.0.0
  108. * @return bool
  109. */
  110. public function is_plugin_page() {
  111. $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false;
  112. if ( empty( $screen ) ) {
  113. return false;
  114. }
  115. return ( ! empty( $screen->id ) && in_array( $screen->id, array( 'plugins', 'plugins-network' ), true ) );
  116. }
  117. /**
  118. * Survey javascript.
  119. *
  120. * @since 1.0.0
  121. */
  122. public function js() {
  123. if ( ! $this->is_plugin_page() ) {
  124. return;
  125. }
  126. ?>
  127. <script type="text/javascript">
  128. jQuery(function($){
  129. var $deactivateLink = $('#the-list').find('[data-slug="<?php echo $this->plugin; ?>"] span.deactivate a'),
  130. $overlay = $('#am-deactivate-survey-<?php echo $this->plugin; ?>'),
  131. $form = $overlay.find('form'),
  132. formOpen = false;
  133. // Plugin listing table deactivate link.
  134. $deactivateLink.on('click', function(event) {
  135. event.preventDefault();
  136. $overlay.css('display', 'table');
  137. formOpen = true;
  138. $form.find('.am-deactivate-survey-option:first-of-type input[type=radio]').focus();
  139. });
  140. // Survey radio option selected.
  141. $form.on('change', 'input[type=radio]', function(event) {
  142. event.preventDefault();
  143. $form.find('input[type=text], .error').hide();
  144. $form.find('.am-deactivate-survey-option').removeClass('selected');
  145. $(this).closest('.am-deactivate-survey-option').addClass('selected').find('input[type=text]').show();
  146. });
  147. // Survey Skip & Deactivate.
  148. $form.on('click', '.am-deactivate-survey-deactivate', function(event) {
  149. event.preventDefault();
  150. location.href = $deactivateLink.attr('href');
  151. });
  152. // Survey submit.
  153. $form.submit(function(event) {
  154. event.preventDefault();
  155. if (! $form.find('input[type=radio]:checked').val()) {
  156. $form.find('.am-deactivate-survey-footer').prepend('<span class="error"><?php echo esc_js( __( 'Please select an option', 'google-analytics-for-wordpress' ) ); ?></span>');
  157. return;
  158. }
  159. var data = {
  160. code: $form.find('.selected input[type=radio]').val(),
  161. reason: $form.find('.selected .am-deactivate-survey-option-reason').text(),
  162. details: $form.find('.selected input[type=text]').val(),
  163. site: '<?php echo esc_url( home_url() ); ?>',
  164. plugin: '<?php echo sanitize_key( $this->name ); ?>'
  165. }
  166. var submitSurvey = $.post('<?php echo $this->api_url; ?>', data);
  167. submitSurvey.always(function() {
  168. location.href = $deactivateLink.attr('href');
  169. });
  170. });
  171. // Exit key closes survey when open.
  172. $(document).keyup(function(event) {
  173. if (27 === event.keyCode && formOpen) {
  174. $overlay.hide();
  175. formOpen = false;
  176. $deactivateLink.focus();
  177. }
  178. });
  179. });
  180. </script>
  181. <?php
  182. }
  183. /**
  184. * Survey CSS.
  185. *
  186. * @since 1.0.0
  187. */
  188. public function css() {
  189. if ( ! $this->is_plugin_page() ) {
  190. return;
  191. }
  192. ?>
  193. <style type="text/css">
  194. .am-deactivate-survey-modal {
  195. display: none;
  196. table-layout: fixed;
  197. position: fixed;
  198. z-index: 9999;
  199. width: 100%;
  200. height: 100%;
  201. text-align: center;
  202. font-size: 14px;
  203. top: 0;
  204. left: 0;
  205. background: rgba(0,0,0,0.8);
  206. }
  207. .am-deactivate-survey-wrap {
  208. display: table-cell;
  209. vertical-align: middle;
  210. }
  211. .am-deactivate-survey {
  212. background-color: #fff;
  213. max-width: 550px;
  214. margin: 0 auto;
  215. padding: 30px;
  216. text-align: left;
  217. }
  218. .am-deactivate-survey .error {
  219. display: block;
  220. color: red;
  221. margin: 0 0 10px 0;
  222. }
  223. .am-deactivate-survey-title {
  224. display: block;
  225. font-size: 18px;
  226. font-weight: 700;
  227. text-transform: uppercase;
  228. border-bottom: 1px solid #ddd;
  229. padding: 0 0 18px 0;
  230. margin: 0 0 18px 0;
  231. }
  232. .am-deactivate-survey-title span {
  233. color: #999;
  234. margin-right: 10px;
  235. }
  236. .am-deactivate-survey-desc {
  237. display: block;
  238. font-weight: 600;
  239. margin: 0 0 18px 0;
  240. }
  241. .am-deactivate-survey-option {
  242. margin: 0 0 10px 0;
  243. }
  244. .am-deactivate-survey-option-input {
  245. margin-right: 10px !important;
  246. }
  247. .am-deactivate-survey-option-details {
  248. display: none;
  249. width: 90%;
  250. margin: 10px 0 0 30px;
  251. }
  252. .am-deactivate-survey-footer {
  253. margin-top: 18px;
  254. }
  255. .am-deactivate-survey-deactivate {
  256. float: right;
  257. font-size: 13px;
  258. color: #ccc;
  259. text-decoration: none;
  260. padding-top: 7px;
  261. }
  262. </style>
  263. <?php
  264. }
  265. /**
  266. * Survey modal.
  267. *
  268. * @since 1.0.0
  269. */
  270. public function modal() {
  271. if ( ! $this->is_plugin_page() ) {
  272. return;
  273. }
  274. $options = array(
  275. 1 => array(
  276. 'title' => esc_html__( 'I no longer need the plugin', 'google-analytics-for-wordpress' ),
  277. ),
  278. 2 => array(
  279. 'title' => esc_html__( 'I\'m switching to a different plugin', 'google-analytics-for-wordpress' ),
  280. 'details' => esc_html__( 'Please share which plugin', 'google-analytics-for-wordpress' ),
  281. ),
  282. 3 => array(
  283. 'title' => esc_html__( 'I couldn\'t get the plugin to work', 'google-analytics-for-wordpress' ),
  284. ),
  285. 4 => array(
  286. 'title' => esc_html__( 'It\'s a temporary deactivation', 'google-analytics-for-wordpress' ),
  287. ),
  288. 5 => array(
  289. 'title' => esc_html__( 'Other', 'google-analytics-for-wordpress' ),
  290. 'details' => esc_html__( 'Please share the reason', 'google-analytics-for-wordpress' ),
  291. ),
  292. );
  293. ?>
  294. <div class="am-deactivate-survey-modal" id="am-deactivate-survey-<?php echo $this->plugin; ?>">
  295. <div class="am-deactivate-survey-wrap">
  296. <form class="am-deactivate-survey" method="post">
  297. <span class="am-deactivate-survey-title"><span class="dashicons dashicons-testimonial"></span><?php echo ' ' . esc_html__( 'Quick Feedback', 'google-analytics-for-wordpress' ); ?></span>
  298. <span class="am-deactivate-survey-desc"><?php echo sprintf( esc_html__('If you have a moment, please share why you are deactivating %s:', 'google-analytics-for-wordpress' ), $this->name ); ?></span>
  299. <div class="am-deactivate-survey-options">
  300. <?php foreach ( $options as $id => $option ) : ?>
  301. <div class="am-deactivate-survey-option">
  302. <label for="am-deactivate-survey-option-<?php echo $this->plugin; ?>-<?php echo $id; ?>" class="am-deactivate-survey-option-label">
  303. <input id="am-deactivate-survey-option-<?php echo $this->plugin; ?>-<?php echo $id; ?>" class="am-deactivate-survey-option-input" type="radio" name="code" value="<?php echo $id; ?>" />
  304. <span class="am-deactivate-survey-option-reason"><?php echo $option['title']; ?></span>
  305. </label>
  306. <?php if ( ! empty( $option['details'] ) ) : ?>
  307. <input class="am-deactivate-survey-option-details" type="text" placeholder="<?php echo $option['details']; ?>" />
  308. <?php endif; ?>
  309. </div>
  310. <?php endforeach; ?>
  311. </div>
  312. <div class="am-deactivate-survey-footer">
  313. <button type="submit" class="am-deactivate-survey-submit button button-primary button-large"><?php echo sprintf( esc_html__('Submit %s Deactivate', 'google-analytics-for-wordpress' ), '&amp;' ); ?></button>
  314. <a href="#" class="am-deactivate-survey-deactivate"><?php echo sprintf( esc_html__('Skip %s Deactivate', 'google-analytics-for-wordpress' ), '&amp;' ); ?></a>
  315. </div>
  316. </form>
  317. </div>
  318. </div>
  319. <?php
  320. }
  321. }
  322. } // End if().