PageRenderTime 25ms CodeModel.GetById 9ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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