<?php
defined( 'ABSPATH' ) || exit();

class WPCP_Pro_Free_Installer {
	/**
	 * WPCP_Pro_Free_Installer constructor.
	 */
	public function __construct() {
		add_action( 'admin_notices', array( $this, 'admin_notice' ) );
		add_action( 'admin_print_scripts', array( $this, 'installation_script' ), 99 );
		add_action( 'wp_ajax_wpcp_install_base_plugin', array( $this, 'install_plugin' ) );
	}


	/**
	 * @since 1.0.0
	 */
	public function admin_notice() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if ( function_exists( 'content_pilot' ) ) {
			return;
		}

		$button           = sprintf( "<a href='#' id='install-wpcp' class='button button-secondary'>%s</a>", __( 'Install & Activate', 'wp-content-pilot-pro' ) );
		$free_plugin_link = '<a href="https://wordpress.org/plugins/wp-content-pilot/" target="_blank">WP Content Pilot</a>';
		$message          = sprintf( __( 'You are one step ahead of using <strong>WP Content Pilot PRO</strong>, the  plugin requires <strong>%s</strong> installed & Activated. %s', 'wp-content-pilot-pro' ), $free_plugin_link, $button );
		$class            = 'notice notice-error';
		printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), $message );

	}


	public function installation_script() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if ( function_exists( 'content_pilot' ) ) {
			return;
		}
		ob_start();
		?>

        <script>
            jQuery(document).ready(function () {
                jQuery('#install-wpcp').on('click', function (e) {
                    e.preventDefault();
                    jQuery(this).text('Installing Please Wait.....');
                    jQuery(this).attr('disabled', 'disabled');

                    jQuery.ajax({
                        type: 'POST',
                        url: '<?php echo esc_url( admin_url( 'admin-ajax.php' ) );?>',
                        data: {
                            action: 'wpcp_install_base_plugin',
                            nonce: '<?php echo wp_create_nonce( 'wpcp_install_base_plugin' );?>',
                            dataType: 'json'
                        },
                        success: function (data) {
                            location.reload();
                        },
                        error: function (xhr, status, error) {
                            alert(error);
                        }
                    });
                });
            });
        </script>


		<?php
		$output = ob_get_contents();
		ob_get_clean();
		echo $output;
	}

	/**
	 * @since 1.0.0
	 */
	public function install_plugin() {
		if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'wpcp_install_base_plugin' ) ) {
			wp_send_json_error( 'Cheating???' );
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if ( function_exists( 'content_pilot' ) ) {
			return;
		}

		if ( self::is_inactive() ) {
			activate_plugin( 'wp-content-pilot/wp-content-pilot.php' );
			wp_send_json_success();
		}

		self::install();
		wp_send_json_success();
	}


	/**
	 * @since 1.0.0
	 */
	public static function is_inactive() {
		include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

		return is_plugin_inactive( 'wp-content-pilot/wp-content-pilot.php' );
	}


	/**
	 * @since 1.0.0
	 */
	public static function install() {
		// Include required libs for installation
		require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
		require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
		require_once( ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php' );
		require_once( ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php' );
		// Get Plugin Info
		$api      = plugins_api( 'plugin_information',
			array(
				'slug'   => 'wp-content-pilot-pro',
				'fields' => array(
					'short_description' => false,
					'sections'          => false,
					'requires'          => false,
					'rating'            => false,
					'ratings'           => false,
					'downloaded'        => false,
					'last_updated'      => false,
					'added'             => false,
					'tags'              => false,
					'compatibility'     => false,
					'homepage'          => false,
					'donate_link'       => false,
				),
			)
		);
		$skin     = new WP_Ajax_Upgrader_Skin();
		$upgrader = new Plugin_Upgrader( $skin );
		$upgrader->install( $api->download_link );
		activate_plugin( 'wp-content-pilot/wp-content-pilot.php' );
	}

}

new WPCP_Pro_Free_Installer();
