class-imagify-partner.php 21.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
<?php
/**
 * Tool allowing 3rd party WordPress plugins to handle partnership with Imagify.
 *
 * @package wp-media/wp-imagify-partner
 */

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'Imagify_Partner' ) ) :

	/**
	 * Class allowing to download, install, and activate Imagify plugin.
	 *
	 * @author Grégory Viguier
	 */
	class Imagify_Partner {

		/**
		 * Class version.
		 *
		 * @var string
		 */
		const VERSION = '1.0';

		/**
		 * Name of the option that stores the partner identifier.
		 *
		 * @var string
		 */
		const OPTION_NAME = 'imagifyp_id';

		/**
		 * Name of the transient that stores the error messages.
		 *
		 * @var string
		 */
		const ERROR_TRANSIENT_NAME = 'imagifyp_error';

		/**
		 * Name of the URL argument used on success.
		 *
		 * @var string
		 */
		const SUCCESS_ARG = 'imp-success';

		/**
		 * Name of the URL argument used to display an error notice.
		 *
		 * @var string
		 */
		const ERROR_ARG = 'imp-error';

		/**
		 * ID of the nonce used to install Imagify.
		 *
		 * @var string
		 */
		const NONCE_NAME = 'install_imagify_from_partner';

		/**
		 * Message used as fallback in get_message().
		 *
		 * @var string
		 */
		const FALLBACK_MESSAGE = 'Unknown message';

		/**
		 * Partner identifier.
		 *
		 * @var    string
		 * @access protected
		 */
		protected $partner;


		/** ----------------------------------------------------------------------------------------- */
		/** INSTANCE, INIT ========================================================================== */
		/** ----------------------------------------------------------------------------------------- */

		/**
		 * Class constructor: sanitize and set the partner identifier.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 *
		 * @param string $partner Partner identifier.
		 */
		public function __construct( $partner ) {
			$this->partner = self::sanitize_partner( $partner );
		}

		/**
		 * Class init.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 */
		public function init() {
			if ( ! $this->get_partner() ) {
				return;
			}

			if ( ! is_admin() ) {
				return;
			}

			if ( ! self::has_imagify_api_key() ) {
				add_action( 'wp_ajax_' . $this->get_post_action(),    array( $this, 'post_callback' ) );
				add_action( 'admin_post_' . $this->get_post_action(), array( $this, 'post_callback' ) );
			}

			if ( self::is_success() || self::is_error() ) {
				add_action( 'all_admin_notices',    array( __CLASS__, 'error_notice' ) );
				add_filter( 'removable_query_args', array( __CLASS__, 'add_query_args' ) );
			}
		}


		/** ----------------------------------------------------------------------------------------- */
		/** MAIN PUBLIC TOOLS ======================================================================= */
		/** ----------------------------------------------------------------------------------------- */

		/**
		 * Tell if Imagify's API key is set.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 *
		 * @return bool
		 */
		public static function has_imagify_api_key() {
			static $has;

			if ( isset( $has ) ) {
				return $has;
			}

			if ( function_exists( 'get_imagify_option' ) ) {
				// Imagify is already installed and activated.
				$has = (bool) get_imagify_option( 'api_key' );
				return $has;
			}

			if ( defined( 'IMAGIFY_API_KEY' ) && IMAGIFY_API_KEY ) {
				// It's defined in wp-config.php.
				$has = true;
				return $has;
			}

			if ( ! is_multisite() ) {
				// Monosite: grab the value from the options table.
				$options = get_option( 'imagify_settings' );
				$has     = ! empty( $options['api_key'] );
				return $has;
			}

			$options = get_site_option( 'imagify_settings' );

			if ( ! empty( $options['api_key'] ) ) {
				// Multisite: Imagify was activated in the network.
				$has = true;
				return $has;
			}

			// Multisite: Imagify was activated for this site.
			$options = get_option( 'imagify_settings' );
			$has     = ! empty( $options['api_key'] );
			return $has;
		}

		/**
		 * Tell if Imagify is activated.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 *
		 * @return bool
		 */
		public static function is_imagify_activated() {
			return defined( 'IMAGIFY_VERSION' );
		}

		/**
		 * Tell if Imagify is installed.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 *
		 * @return bool
		 */
		public static function is_imagify_installed() {
			if ( self::is_imagify_activated() ) {
				return true;
			}

			return file_exists( self::get_imagify_path() );
		}

		/**
		 * Tell if Imagify has been successfully installed.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 *
		 * @return bool
		 */
		public static function is_success() {
			return ! empty( $_GET[ self::SUCCESS_ARG ] ); // WPCS: CSRF ok.
		}

		/**
		 * Tell if Imagify install failed.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 *
		 * @return bool
		 */
		public static function is_error() {
			return ! empty( $_GET[ self::ERROR_ARG ] ); // WPCS: CSRF ok.
		}

		/**
		 * Get the URL to install and activate Imagify.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 *
		 * @return string The URL.
		 */
		public function get_post_install_url() {
			if ( ! $this->get_partner() || ! self::current_user_can() ) {
				return '';
			}

			$install_url = admin_url( 'admin-post.php' );
			$args        = array(
				'action'           => $this->get_post_action(),
				'_wpnonce'         => wp_create_nonce( self::NONCE_NAME ),
				// To make sure we have a referrer.
				'_wp_http_referer' => rawurlencode( self::get_current_url() ),
			);

			return add_query_arg( $args, $install_url );
		}

		/**
		 * Get the partner identifier.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 *
		 * @return string Partner identifier.
		 */
		public function get_partner() {
			return $this->partner;
		}


		/** ----------------------------------------------------------------------------------------- */
		/** HOOKS =================================================================================== */
		/** ----------------------------------------------------------------------------------------- */

		/**
		 * Post callback to install and activate Imagify.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 */
		public function post_callback() {
			if ( ! check_ajax_referer( self::NONCE_NAME, '_wpnonce', false ) ) {
				$this->error_die();
			}

			if ( ! self::current_user_can() ) {
				$this->error_die( 'cant_install' );
			}

			/**
			 * Store the partner ID before doing anything.
			 * If something goes wrong during the plugin installation, the partner ID will still be saved.
			 */
			self::store_partner( $this->get_partner() );

			// Install Imagify.
			$result = $this->install_imagify();

			if ( is_wp_error( $result ) ) {
				// Install failed.
				if ( self::doing_ajax() ) {
					$this->send_json_error( $result );
				}
				// Redirect to the plugins search page.
				$this->error_redirect( $result );
			}

			// Activate Imagify.
			$result = $this->activate_imagify();

			if ( is_wp_error( $result ) ) {
				// Activation failed.
				if ( self::doing_ajax() ) {
					$this->send_json_error( $result );
				}
				// Redirect to the plugins search page.
				$this->error_redirect( $result );
			}

			if ( self::doing_ajax() ) {
				$this->send_json_success();
			}
			// Redirect to the partner's page.
			$this->success_redirect();
		}

		/**
		 * Maybe print an error notice on the plugins install page.
		 * We add the query argument we use to display an error message.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 */
		public static function error_notice() {
			if ( ! self::is_error() ) {
				// No URL argument.
				return;
			}

			$screen = get_current_screen();

			if ( ! $screen || 'plugin-install' !== $screen->id ) {
				// Not the good page.
				return;
			}

			$partner = self::get_stored_partner();

			if ( ! $partner ) {
				// No partner stored in the database.
				return;
			}

			$errors = get_transient( self::ERROR_TRANSIENT_NAME );

			if ( ! $errors ) {
				// No error messages.
				return;
			}

			if ( ! is_wp_error( $errors ) ) {
				// Invalid value.
				delete_transient( self::ERROR_TRANSIENT_NAME );
				return;
			}

			$errors = $errors->get_error_messages();

			if ( $errors ) {
				foreach ( $errors as $i => $error ) {
					if ( self::FALLBACK_MESSAGE === $error ) {
						unset( $errors[ $i ] );
					}
				}
			}

			if ( ! $errors ) {
				// Add a generic message.
				$instance = new self( $partner );
				$errors[] = $instance->get_message( 'process_failed' );
			}

			echo '<div class="error notice is-dismissible"><p>' . implode( '<br/>', $errors ) . '</p></div>';
		}

		/**
		 * Filter the list of query variables to remove from admin area URLs.
		 * We add the query arguments we use on success or error.
		 *
		 * @since  1.0
		 * @access public
		 * @see    wp_removable_query_args()
		 * @author Grégory Viguier
		 *
		 * @param  array $removable_query_args An array of query variables to remove from a URL.
		 * @return array
		 */
		public static function add_query_args( $removable_query_args ) {
			$removable_query_args[] = self::SUCCESS_ARG;
			$removable_query_args[] = self::ERROR_ARG;
			return $removable_query_args;
		}


		/** ----------------------------------------------------------------------------------------- */
		/** INFOS, INSTALL, ACTIVATE ================================================================ */
		/** ----------------------------------------------------------------------------------------- */

		/**
		 * Get Imagify infos from the repository.
		 *
		 * @since  1.0
		 * @access protected
		 * @author Grégory Viguier
		 *
		 * @return object The plugin infos on success. A WP_Error object on failure.
		 */
		protected function get_imagify_infos() {
			static $infos;

			if ( isset( $infos ) ) {
				return $infos;
			}

			require_once ABSPATH . 'wp-admin/includes/plugin-install.php';

			// Get Plugin Infos.
			$infos = plugins_api( 'plugin_information', array(
				'slug'   => 'imagify',
				'fields' => array(
					'short_description' => false,
					'sections'          => false,
					'rating'            => false,
					'ratings'           => false,
					'downloaded'        => false,
					'last_updated'      => false,
					'added'             => false,
					'tags'              => false,
					'homepage'          => false,
					'donate_link'       => false,
				),
			) );

			return $infos;
		}

		/**
		 * Get the URL to download Imagify.
		 *
		 * @since  1.0
		 * @access protected
		 * @author Grégory Viguier
		 *
		 * @return string The URL. An empty string on error.
		 */
		protected function get_download_url() {
			$infos = $this->get_imagify_infos();
			return ! empty( $infos->download_link ) ? $infos->download_link : '';
		}

		/**
		 * Install Imagify.
		 *
		 * @since  1.0
		 * @access protected
		 * @author Grégory Viguier
		 *
		 * @return object|null A WP_Object on failure, null on success.
		 */
		protected function install_imagify() {
			if ( self::is_imagify_installed() ) {
				// Imagify is already installed.
				return null;
			}

			$infos = $this->get_imagify_infos();

			if ( is_wp_error( $infos ) ) {
				return $infos;
			}

			ob_start();
			@set_time_limit( 0 );

			require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

			$upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() );
			$result   = $upgrader->install( $this->get_download_url() );

			ob_end_clean();

			if ( is_wp_error( $result ) ) {
				return $result;
			}

			clearstatcache();

			if ( ! self::is_imagify_installed() ) {
				return new WP_Error( 'process_failed', $this->get_message( 'process_failed' ) );
			}

			return null;
		}

		/**
		 * Activate Imagify.
		 *
		 * @since  1.0
		 * @access protected
		 * @author Grégory Viguier
		 *
		 * @return object|null A WP_Object on failure, null on success.
		 */
		protected function activate_imagify() {
			return activate_plugin( self::get_imagify_path(), false, is_multisite() );
		}

		/**
		 * Get a message used by the class.
		 *
		 * @since  1.0
		 * @access protected
		 * @author Grégory Viguier
		 *
		 * @param  string $message_id A message ID.
		 * @return string             A message.
		 */
		protected function get_message( $message_id ) {
			$messages = array(
				'success'        => __( 'Plugin installed successfully.' ),
				'cant_install'   => __( 'Sorry, you are not allowed to install plugins on this site.' ),
				'not_allowed'    => __( 'Sorry, you are not allowed to do that.' ),
				'process_failed' => __( 'Plugin install failed.' ),
				'go_back'        => __( 'Go back' ),
			);

			/**
			 * Filter messages used everywhere in the class.
			 * Default messages are already translated by WordPress.
			 *
			 * @since  1.0
			 * @author Grégory Viguier
			 *
			 * @param array $messages Messages.
			 */
			$messages = apply_filters( 'imagify_partner_messages_' . $this->get_partner(), $messages );

			return ! empty( $messages[ $message_id ] ) ? $messages[ $message_id ] : self::FALLBACK_MESSAGE;
		}


		/** ----------------------------------------------------------------------------------------- */
		/** HANDLE SUCCESS ========================================================================== */
		/** ----------------------------------------------------------------------------------------- */

		/**
		 * Send a JSON response back to an Ajax request, indicating success.
		 *
		 * @since  1.0
		 * @access protected
		 * @author Grégory Viguier
		 */
		protected function send_json_success() {
			delete_transient( self::ERROR_TRANSIENT_NAME );

			wp_send_json_success( $this->get_message( 'success' ) );
		}

		/**
		 * Redirect the user after Imagify is successfully installed and activated.
		 *
		 * @since  1.0
		 * @access protected
		 * @author Grégory Viguier
		 */
		protected function success_redirect() {
			delete_transient( self::ERROR_TRANSIENT_NAME );

			wp_safe_redirect( esc_url_raw( $this->get_success_redirection_url() ) );
			die();
		}

		/**
		 * Get the URL to redirect the user to after Imagify is successfully installed and activated: the referrer (partner's page URL).
		 * A "success" argument is added.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 *
		 * @return string
		 */
		public function get_success_redirection_url() {
			$success_url = add_query_arg( array(
				self::SUCCESS_ARG => 1,
				self::ERROR_ARG   => false,
			), wp_get_referer() );

			/**
			 * Filter the URL to redirect the user to after Imagify is successfully installed and activated.
			 * Default is the partner's page URL.
			 *
			 * @since  1.0
			 * @author Grégory Viguier
			 *
			 * @param string $success_url The URL.
			 */
			return apply_filters( 'imagify_partner_success_url_' . $this->get_partner(), $success_url );
		}


		/** ----------------------------------------------------------------------------------------- */
		/** HANDLE ERROR ============================================================================ */
		/** ----------------------------------------------------------------------------------------- */

		/**
		 * Die on error.
		 *
		 * @since  1.0
		 * @access protected
		 * @author Grégory Viguier
		 *
		 * @param string $message_id An error message ID.
		 */
		protected function error_die( $message_id = 'not_allowed' ) {
			$message = $this->get_message( $message_id );

			if ( self::doing_ajax() ) {
				$message = new WP_Error( $message_id, $message );
				$this->send_json_error( $message );
			}

			if ( wp_get_referer() ) {
				$message .= '</p><p>';
				$message .= sprintf( '<a href="%s">%s</a>',
					esc_url( remove_query_arg( 'updated', wp_get_referer() ) ),
					$this->get_message( 'go_back' )
				);
			}

			wp_die( $message, '', 403 );
		}

		/**
		 * Send a JSON response back to an Ajax request, indicating failure.
		 * This is a backward compatible version of wp_send_json_error(): WP_Error object handling was introduced in WP 4.1.
		 *
		 * @since  1.0
		 * @access protected
		 * @author Grégory Viguier
		 *
		 * @param mixed $data Data to encode as JSON, then print and die.
		 */
		protected function send_json_error( $data ) {
			if ( is_wp_error( $data ) ) {
				$result = array();
				foreach ( $data->errors as $code => $messages ) {
					foreach ( $messages as $message ) {
						$result[] = array(
							'code'    => $code,
							'message' => $message,
						);
					}
				}
			} else {
				$result = $data;
			}

			wp_send_json_error( $result );
		}

		/**
		 * Store an error message in a transient then redirect the user.
		 *
		 * @since  1.0
		 * @access protected
		 * @author Grégory Viguier
		 *
		 * @param object $error A WP_Error object.
		 */
		protected function error_redirect( $error ) {
			set_transient( self::ERROR_TRANSIENT_NAME, $error, 30 );

			wp_safe_redirect( esc_url_raw( $this->get_error_redirection_url() ) );
			die();
		}

		/**
		 * Get the URL to redirect the user to after Imagify installation failure: the plugins search page URL, searching for Imagify.
		 * An "error" argument is added, to display an error notice.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 *
		 * @return string
		 */
		public function get_error_redirection_url() {
			$error_url = 'plugin-install.php?s=imagify&tab=search&type=term&' . self::ERROR_ARG . '=1';
			$error_url = is_multisite() ? network_admin_url( $error_url ) : admin_url( $error_url );

			/**
			 * Filter the URL to redirect the user to after Imagify installation failure.
			 * Default is the plugins search page URL.
			 *
			 * @since  1.0
			 * @author Grégory Viguier
			 *
			 * @param string $error_url The URL.
			 */
			return apply_filters( 'imagify_partner_error_url_' . $this->get_partner(), $error_url );
		}


		/** ----------------------------------------------------------------------------------------- */
		/** STORING THE PARTNER ID IN DATABASE ====================================================== */
		/** ----------------------------------------------------------------------------------------- */

		/**
		 * Get the partner identifier stored in the Database.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 *
		 * @return string|bool The partner identifier, or false if none is stored.
		 */
		public static function get_stored_partner() {
			$partner = get_option( self::OPTION_NAME );

			if ( $partner && is_string( $partner ) ) {
				$partner = self::sanitize_partner( $partner );
			}

			return $partner ? $partner : false;
		}

		/**
		 * Delete the partner identifier stored in the Database.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 */
		public static function delete_stored_partner() {
			if ( false !== get_option( self::OPTION_NAME ) ) {
				delete_option( self::OPTION_NAME );
			}
		}

		/**
		 * Store the partner identifier in Database.
		 *
		 * @since  1.0
		 * @access protected
		 * @author Grégory Viguier
		 *
		 * @param string $partner The partner identifier to store.
		 */
		protected static function store_partner( $partner ) {
			if ( false === get_option( self::OPTION_NAME ) ) {
				add_option( self::OPTION_NAME, $partner );
			} else {
				update_option( self::OPTION_NAME, $partner );
			}
		}

		/**
		 * Sanitize a partner ID.
		 *
		 * @since  1.0
		 * @access protected
		 * @author Grégory Viguier
		 *
		 * @param  string $partner Partner identifier.
		 * @return string
		 */
		protected static function sanitize_partner( $partner ) {
			return preg_replace( '@[^a-z0-9_-]@', '', strtolower( (string) $partner ) );
		}


		/** ----------------------------------------------------------------------------------------- */
		/** VARIOUS TOOLS =========================================================================== */
		/** ----------------------------------------------------------------------------------------- */

		/**
		 * Get the action.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 *
		 * @return string Partner identifier.
		 */
		public function get_post_action() {
			return 'install_imagify_from_partner_' . $this->get_partner();
		}

		/**
		 * Determines whether the current request is a WordPress Ajax request.
		 * This is a clone of wp_doing_ajax(), intriduced in WP 4.7.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 *
		 * @return bool True if it's a WordPress Ajax request, false otherwise.
		 */
		public static function doing_ajax() {
			/**
			 * Filters whether the current request is a WordPress Ajax request.
			 *
			 * @since 1.0
			 *
			 * @param bool $wp_doing_ajax Whether the current request is a WordPress Ajax request.
			 */
			return apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX );
		}

		/**
		 * Get Imagify's file path.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 *
		 * @return string The file path.
		 */
		public static function get_imagify_path() {
			if ( defined( 'IMAGIFY_FILE' ) ) {
				return IMAGIFY_FILE;
			}

			return WP_PLUGIN_DIR . '/imagify/imagify.php';
		}

		/**
		 * Tell if the current user can install and activate Imagify.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 *
		 * @return bool
		 */
		public static function current_user_can() {
			static $can;

			if ( ! isset( $can ) ) {
				$can = is_multisite() ? 'manage_network_plugins' : 'install_plugins';
				$can = current_user_can( $can );
			}

			return $can;
		}

		/**
		 * Get the current URL.
		 *
		 * @since  1.0
		 * @access public
		 * @author Grégory Viguier
		 *
		 * @return string
		 */
		public static function get_current_url() {
			$port = (int) $_SERVER['SERVER_PORT'];
			$port = 80 !== $port && 443 !== $port ? ( ':' . $port ) : '';
			$url  = ! empty( $GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI'] ) ? $GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI'] : ( ! empty( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '' );

			return 'http' . ( is_ssl() ? 's' : '' ) . '://' . $_SERVER['HTTP_HOST'] . $port . $url;
		}
	}

endif;