/**
         * This is the callback function for the "install plugin page" above. This creates the output for the install plugins page
         * and uses the mp_core_check_plugins function with the $show_notices set to false. In doing so, it gets an array of each
         * plugin that needs tp be installed so that we can run the MP_CORE_Plugin_Installer class for each plugin.
         *
         * @access   public
         * @since    1.0.0
         * @see      MP_CORE_Plugin_Checker::mp_core_check_plugins()
         * @see      screen_icon()
         * @see      MP_CORE_Plugin_Installer
         * @return   void
         */
        public function mp_core_install_check_callback()
        {
            echo '<div class="wrap">';
            screen_icon();
            echo '<h2>' . __('Installing Required Items.', 'mp_core') . '</h2>';
            //Check plugins and store needed ones in $plugins
            $plugins = $this->mp_core_check_plugins($this->_args, false);
            //If there are no plugins to show, return false.
            if (empty($plugins)) {
                return false;
            }
            //Install_loop_counter
            $install_loop_counter = 0;
            //Loop through each plugin that is supposed to be installed
            foreach ($plugins as $plugin_key => $plugin) {
                $install_loop_counter = $install_loop_counter + 1;
                //If we have installed 3 plugins
                if ($install_loop_counter >= 4) {
                    //Refresh the page so we don't crash anybody's servers with too much in one request. (This hasn't happened yet so for now we'll leave it).
                    /*echo '
                    		<script type="text/javascript">
                    			document.location.reload(true);
                    		</script>';
                    		
                    		exit;*/
                }
                //If this plugin requires a license to be installed
                if ($plugin['plugin_licensed']) {
                    $plugin_name_slug = sanitize_title($plugin['plugin_name']);
                    //Listen for our Submit License button to be clicked
                    if (isset($_POST[$plugin_name_slug . '_license_key'])) {
                        //If it has, store it in the license_key variable
                        $license_key = $_POST[$plugin_name_slug . '_license_key'];
                        //Check nonce
                        if (!check_admin_referer($plugin_name_slug . '_nonce', $plugin_name_slug . '_nonce')) {
                            return false;
                        }
                        // get out if we didn't click the Activate button
                        $args = array('software_name' => $plugin['plugin_name'], 'software_api_url' => $plugin['plugin_api_url'], 'software_license_key' => $license_key, 'software_store_license' => true);
                        mp_core_verify_license($args);
                    }
                    //If this plugin could use a different/parent plugin's license (this is likely an add-on plugin).
                    //If this passes, it just means the user doesn't have to enter the same license again
                    if (!empty($plugin['plugin_licensed_parent_name'])) {
                        $parent_plugin_name_slug = sanitize_title($plugin['plugin_licensed_parent_name']);
                        //Get the previously saved license for the parent plugin from the database
                        $license_key = get_option($parent_plugin_name_slug . '_license_key');
                        //If there's no parent license key entered, see if there's one entered for this plugin
                        $license_key = empty($license_key) ? get_option($plugin_name_slug . '_license_key') : $license_key;
                        $verify_license_args = array('software_name' => $plugin['plugin_name'], 'software_api_url' => $plugin['plugin_api_url'], 'software_license_key' => $license_key, 'software_store_license' => true);
                        //If the previously saved parent's license from the database is valid for this add-on
                        if (mp_core_verify_license($verify_license_args)) {
                            $plugin['plugin_license'] = $license_key;
                            //Install and activate this plugin - right here, right now
                            new MP_CORE_Plugin_Installer($plugin);
                        } else {
                            //Create License Form
                            ?>
							
							<div id="<?php 
                            echo $plugin_name_slug;
                            ?>
-plugin-license-wrap" class="wrap mp-core-plugin-license-wrap">
								
                                <?php 
                            //If there is nothing entered for the license key, tell the user they need one.
                            if (empty($license_key)) {
                                ?>
									<p class="plugin-description"><?php 
                                echo __("You need a license for ", 'mp_core') . $plugin['plugin_name'];
                                ?>
</p><?php 
                            } else {
                                ?>
									<p class="plugin-description"><?php 
                                echo __("The license key is invalid for ", 'mp_core') . $plugin['plugin_name'];
                                ?>
</p><?php 
                            }
                            ?>
                                
								<form method="post">
													
									<input style="float:left; margin-right:10px;" id="<?php 
                            echo $plugin_name_slug;
                            ?>
_license_key" name="<?php 
                            echo $plugin_name_slug;
                            ?>
_license_key" type="text" class="regular-text" value="<?php 
                            esc_attr_e($license_key);
                            ?>
" />						
									
									<?php 
                            wp_nonce_field($plugin_name_slug . '_nonce', $plugin_name_slug . '_nonce');
                            ?>
											
									<br />
										
									<?php 
                            submit_button(__('Submit License', 'mp_core'));
                            ?>
								
								</form>
							</div>
					   
							<?php 
                        }
                    } else {
                        //Check if there's a license waiting in the $_POST var for this plugin
                        if (isset($_POST[$plugin_name_slug . '_license_key'])) {
                            //If there is a submitted license, store it in the license_key variable
                            $license_key = $_POST[$plugin_name_slug . '_license_key'];
                            //Check nonce
                            if (!check_admin_referer($plugin_name_slug . '_nonce', $plugin_name_slug . '_nonce')) {
                                return false;
                            }
                            // get out if we didn't click the Activate button
                            $verify_license_args = array('software_name' => $plugin['plugin_name'], 'software_api_url' => $plugin['plugin_api_url'], 'software_license_key' => $license_key, 'software_store_license' => true);
                            //If this license is valid
                            if (mp_core_verify_license($verify_license_args)) {
                                $plugin['plugin_license'] = $license_key;
                                //Install and activate this plugin - right here, right now
                                new MP_CORE_Plugin_Installer($plugin);
                            } else {
                                //Create License Form
                                ?>
								
								<div id="<?php 
                                echo $plugin_name_slug;
                                ?>
-plugin-license-wrap" class="wrap mp-core-plugin-license-wrap">
									
									<p class="plugin-description"><?php 
                                echo __("You need a license for ", 'mp_core') . $plugin['plugin_name'];
                                ?>
</p>
									
									<form method="post">
														
										<input style="float:left; margin-right:10px;" id="<?php 
                                echo $plugin_name_slug;
                                ?>
_license_key" name="<?php 
                                echo $plugin_name_slug;
                                ?>
_license_key" type="text" class="regular-text" value="<?php 
                                esc_attr_e($license_key);
                                ?>
" />						
										
										<?php 
                                wp_nonce_field($plugin_name_slug . '_nonce', $plugin_name_slug . '_nonce');
                                ?>
												
										<br />
											
										<?php 
                                submit_button(__('Submit License', 'mp_core'));
                                ?>
									
									</form>
								</div>
						   
								<?php 
                            }
                        } else {
                            //Get the previously saved license from the database
                            $license_key = get_option($plugin_name_slug . '_license_key');
                            $verify_license_args = array('software_name' => $plugin['plugin_name'], 'software_api_url' => $plugin['plugin_api_url'], 'software_license_key' => $license_key, 'software_store_license' => false);
                            //If the previously saved license from the database is valid
                            if (mp_core_verify_license($verify_license_args)) {
                                $plugin['plugin_license'] = $license_key;
                                //Install and activate this plugin - right here, right now
                                new MP_CORE_Plugin_Installer($plugin);
                            } else {
                                //Create License Form
                                ?>
								
								<div id="<?php 
                                echo $plugin_name_slug;
                                ?>
-plugin-license-wrap" class="wrap mp-core-plugin-license-wrap">
									
									<p class="plugin-description"><?php 
                                echo __("You need a license for ", 'mp_core') . $plugin['plugin_name'];
                                ?>
</p>
									
									<form method="post">
														
										<input style="float:left; margin-right:10px;" id="<?php 
                                echo $plugin_name_slug;
                                ?>
_license_key" name="<?php 
                                echo $plugin_name_slug;
                                ?>
_license_key" type="text" class="regular-text" value="<?php 
                                esc_attr_e($license_key);
                                ?>
" />						
										
										<?php 
                                wp_nonce_field($plugin_name_slug . '_nonce', $plugin_name_slug . '_nonce');
                                ?>
												
										<br />
											
										<?php 
                                submit_button(__('Submit License', 'mp_core'));
                                ?>
									
									</form>
								</div>
						   
								<?php 
                            }
                        }
                    }
                } else {
                    //Install and activate this plugin - right here, right now
                    new MP_CORE_Plugin_Installer($plugin);
                }
            }
            //Set redirect to referring page when complete
            $redirect_after_install_url = $_SERVER['HTTP_REFERER'];
            //Check if we should redirect to the theme page - option is set when new MP theme activated
            $theme_page_redirect = get_option('mp_core_theme_redirect_after_install');
            //If this option has been saved
            if (!empty($theme_page_redirect)) {
                //If theme pages redirect is true
                if ($theme_page_redirect) {
                    //Change redirect url to be the themes page
                    $redirect_after_install_url = admin_url('themes.php');
                }
            }
            /*
            //Javascript for redirection
            echo '<script type="text/javascript">';
            	echo "window.location = '" . $redirect_after_install_url . "';";
            echo '</script>';
            */
            echo '</div>';
            //Reset theme redirect option  to false so new plugins redirect to referrer instead of themes page
            update_option('mp_core_theme_redirect_after_install', false);
        }
/**
 * Dual purpose function which returns a license's previously-saved validity 
 * OR, it listens for a newly posted license in the $_POST and returns its validity - if it exists
 *
 * @access   public
 * @since    1.0.0
 * @see      check_admin_referer()
 * @see      sanitize_title()
 * @see      mp_core_verify_license()
 * @see      get_option()
 * @param    array $args Info about where to check and what license and whether to store it.
 * @return   boolean True if the license is valid, False if not.
 */
function mp_core_listen_for_license_and_get_validity($plugin_args)
{
    $plugin_name_slug = sanitize_title($plugin_args['plugin_name']);
    //If there's a license waiting in the $_POST var for this plugin
    if (isset($_POST[$plugin_name_slug . '_license_key'])) {
        //Check nonce
        if (!check_admin_referer($plugin_name_slug . '_nonce', $plugin_name_slug . '_nonce')) {
            return false;
        }
        // get out if we didn't click the Activate button
        $verify_license_args = array('software_name' => $plugin_args['plugin_name'], 'software_api_url' => $plugin_args['plugin_api_url'], 'software_license_key' => $_POST[$plugin_name_slug . '_license_key'], 'software_store_license' => true);
        //Check and return the validity of this license
        return mp_core_verify_license($verify_license_args);
    } else {
        return get_option($plugin_name_slug . '_license_status_valid');
    }
}
        /**
         * Output the code which will display the license form on the plugins page
         *
         * @access   public
         * @since    1.0.0
         * @see      MP_CORE_Plugin_Updater::parse_the_args()
         * @see      get_option()
         * @see      wp_nonce_field()
         * @see      submit_button()
         * @return   void
         */
        function display_license()
        {
            //Parse the args
            $args = $this->parse_the_args($this->_args);
            //This filter can be used to change the API URL. Useful when calling for updates to the API site's plugins which need to be loaded from a separate URL (see mp_repo_mirror)
            $args['software_api_url'] = has_filter('mp_core_plugin_update_package_url') ? apply_filters('mp_core_plugin_update_package_url', $args['software_api_url']) : $args['software_api_url'];
            //API response
            $api_response = get_site_transient($args['software_name_slug']);
            //If a new license has just been submitted
            if (isset($_POST[$args['software_name_slug'] . '_license_key'])) {
                //Get license from $_POST var
                $license_key = $_POST[$args['software_name_slug'] . '_license_key'];
            } else {
                //Get license from database
                $license_key = get_option($args['software_name_slug'] . '_license_key');
            }
            //Only verify the license if the transient is older than 7 days
            $check_licenses_transient_time = get_site_transient('mp_check_licenses_transient');
            //If our transient is older than 30 days (2592000 seconds)
            if (time() > $check_licenses_transient_time + 2592000) {
                //reset the transient
                set_site_transient('mp_check_licenses_transient', time());
                //Set args to Verfiy the License
                $verify_license_args = array('software_name' => $args['software_name'], 'software_api_url' => $args['software_api_url'], 'software_license_key' => $license_key, 'software_store_license' => true);
                //Double check license. Use the Verfiy License function to verify and store whether this license is valid or not
                mp_core_verify_license($verify_license_args);
            }
            //Get license status (set in verify license class)
            $status = get_option($args['software_name_slug'] . '_license_status_valid');
            //Get license link:
            $get_license_link = !empty($api_response->get_license) ? '<a href="' . $api_response->get_license . '" target="_blank" >' . __('Get License', 'mp_core') . '</a>' : NULL;
            ?>
			<div id="<?php 
            echo $args['software_name_slug'];
            ?>
-plugin-license-wrap" class="wrap mp-core-plugin-license-wrap">
				
				<p class="plugin-description"><?php 
            echo __('Enter your license key to enable automatic updates.', 'mp_core');
            ?>
</p>
				
				<form method="post">
									
					<input style="float:left; margin-right:10px;" id="<?php 
            echo $args['software_name_slug'];
            ?>
_license_key" name="<?php 
            echo $args['software_name_slug'];
            ?>
_license_key" type="text" class="regular-text" value="<?php 
            esc_attr_e($license_key);
            ?>
" />						
					<?php 
            mp_core_true_false_light(array('value' => $status, 'description' => $status == true ? __('Auto-updates enabled.', 'mp_core') : __('This license is not valid! ', 'mp_core') . $get_license_link));
            ?>
					
					<?php 
            wp_nonce_field($args['software_name_slug'] . '_nonce', $args['software_name_slug'] . '_nonce');
            ?>
							
					<br />
						
					<?php 
            submit_button(__('Submit License', 'mp_core'));
            ?>
				
				</form>
			</div>
	   
			<?php 
        }
 /**
  * Setup functions for each plugin in this directory list url
  *
  * @access   public
  * @since    1.0.0
  * @see      wp_remote_post()
  * @see      sanitize_title()
  * @see      MP_CORE_Verify_License
  * @see      get_option()
  * @see      MP_CORE_Plugin_Installer
  * @see      admin_url()
  * @see      wp_create_nonce()
  * @see      add_action()
  * @return   void
  */
 public function setup_functions($directory_list_url)
 {
     //This filter can be used to change the API URL. Useful when calling for updates to the API site's plugins which need to be loaded from a separate URL (see mp_repo_mirror)
     $directory_list_url = has_filter('mp_core_plugin_update_package_url') ? apply_filters('mp_core_plugin_update_package_url', $directory_list_url) : $directory_list_url;
     //Get list of plugins that should be shown
     $response = wp_remote_post($directory_list_url, array('method' => 'POST', 'timeout' => 15, 'sslverify' => false, 'body' => array('directory' => 'true', 'mp_directory_page' => $this->_mp_directory_paged)));
     //Json decode plugins array
     $this->response = json_decode($response['body'], true);
     $this->plugins = $this->response['items'];
     foreach ($this->plugins as $plugin) {
         //Plugin Name Slug
         $plugin_name_slug = sanitize_title($plugin['plugin_name']);
         //EG move-plugins-core
         //If this plugin requires a license
         if ($plugin['plugin_licensed']) {
             //Listen for our activate button to be clicked
             if (isset($_POST[$plugin_name_slug . '_license_key'])) {
                 //If it has, store it in the license_key variable
                 $license_key = $_POST[$plugin_name_slug . '_license_key'];
                 //Check nonce
                 if (!check_admin_referer($plugin_name_slug . '_nonce', $plugin_name_slug . '_nonce')) {
                     return false;
                 }
                 // get out if we didn't click the Activate button
                 $args = array('software_name' => $plugin['plugin_name'], 'software_api_url' => $plugin['plugin_api_url'], 'software_license_key' => $license_key, 'software_store_license' => true);
                 //Store, Verify, and Set the "Green Light" Notification option for this license
                 $license_valid = mp_core_verify_license($args);
             }
         }
         //Get license
         $license = get_option($plugin_name_slug . '_license_key');
         $license_valid = get_option($plugin_name_slug . '_license_status_valid');
         //If the install button for this plugin has been clicked and license entered is valid
         if (isset($_POST[$plugin_name_slug . '_license_key']) && $license_valid) {
             //Redirect to plugin install page
             header('Location: ' . admin_url(sprintf('options-general.php?page=mp_core_install_plugin_page_' . $plugin_name_slug . '&mp-source=mp_core_directory&action=install-plugin&plugin=' . $plugin_name_slug . '&plugin_api_url=' . base64_encode($plugin['plugin_api_url']) . '&mp_core_directory_page=' . $this->_args['slug'] . '&mp_core_directory_tab=' . $this->_mp_directory_tab . '&_wpnonce=%s', wp_create_nonce('install-plugin'))));
         } elseif (isset($_POST[$plugin_name_slug . '_license_key']) && !$license_valid) {
             //Show "License entered not valid" message
             add_action('admin_notices', array($this, 'license_not_valid'));
         }
     }
 }