is_localhost() && current_user_can( 'manage_options' ) ) { // set default strings $default_strings = array( 'connect_title' => __( "Thanks for choosing an AyeCode Product!" ), 'connect_external' => __( "Please confirm you wish to connect your site?" ), 'connect' => sprintf( __( "Have a license? Forget about entering license keys or downloading zip files, connect your site for instant access. %slearn more%s" ), "", "" ), 'connect_button' => __( "Connect Site" ), 'connecting_button' => __( "Connecting..." ), 'error_localhost' => __( "This service will only work with a live domain, not a localhost." ), 'error' => __( "Something went wrong, please refresh and try again." ), ); $this->strings = array_merge( $default_strings, $strings ); // set default pages $default_pages = array(); $this->pages = array_merge( $default_pages, $pages ); // maybe show connect site notice add_action( 'admin_notices', array( $this, 'ayecode_connect_install_notice' ) ); // add ajax action if not already added if ( ! has_action( 'wp_ajax_ayecode_connect_helper' ) ) { add_action( 'wp_ajax_ayecode_connect_helper', array( $this, 'ayecode_connect_install' ) ); } } // add ajax action if not already added if ( ! has_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed' ) ) { add_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed', array( $this, 'ayecode_connect_helper_installed' ) ); } } /** * Give a way to check we can connect via a external redirect. */ public function ayecode_connect_helper_installed(){ $active = array( 'gd' => defined('GEODIRECTORY_VERSION') && version_compare(GEODIRECTORY_VERSION,'2.0.0.79','>') ? 1 : 0, 'uwp' => defined('USERSWP_VERSION') && version_compare(USERSWP_VERSION,'1.2.1.5','>') ? 1 : 0, 'wpi' => defined('WPINV_VERSION') && version_compare(WPINV_VERSION,'1.0.14','>') ? 1 : 0, ); wp_send_json_success( $active ); wp_die(); } /** * Get slug from path * * @param string $key * * @return string */ private function format_plugin_slug( $key ) { $slug = explode( '/', $key ); $slug = explode( '.', end( $slug ) ); return $slug[0]; } /** * Install and activate the AyeCode Connect Plugin */ public function ayecode_connect_install() { // bail if localhost if ( $this->is_localhost() ) { wp_send_json_error( $this->strings['error_localhost'] ); } // Explicitly clear the event. wp_clear_scheduled_hook( 'geodir_plugin_background_installer', func_get_args() ); $success = true; $plugin_slug = "ayecode-connect"; if ( ! empty( $plugin_slug ) ) { require_once( ABSPATH . 'wp-admin/includes/file.php' ); 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/plugin.php' ); WP_Filesystem(); $skin = new Automatic_Upgrader_Skin; $upgrader = new WP_Upgrader( $skin ); $installed_plugins = array_map( array( $this, 'format_plugin_slug' ), array_keys( get_plugins() ) ); $plugin_slug = $plugin_slug; $plugin = $plugin_slug . '/' . $plugin_slug . '.php'; $installed = false; $activate = false; // See if the plugin is installed already if ( in_array( $plugin_slug, $installed_plugins ) ) { $installed = true; $activate = ! is_plugin_active( $plugin ); } // Install this thing! if ( ! $installed ) { // Suppress feedback ob_start(); try { $plugin_information = plugins_api( 'plugin_information', array( 'slug' => $plugin_slug, 'fields' => array( 'short_description' => false, 'sections' => false, 'requires' => false, 'rating' => false, 'ratings' => false, 'downloaded' => false, 'last_updated' => false, 'added' => false, 'tags' => false, 'homepage' => false, 'donate_link' => false, 'author_profile' => false, 'author' => false, ), ) ); if ( is_wp_error( $plugin_information ) ) { throw new Exception( $plugin_information->get_error_message() ); } $package = $plugin_information->download_link; $download = $upgrader->download_package( $package ); if ( is_wp_error( $download ) ) { throw new Exception( $download->get_error_message() ); } $working_dir = $upgrader->unpack_package( $download, true ); if ( is_wp_error( $working_dir ) ) { throw new Exception( $working_dir->get_error_message() ); } $result = $upgrader->install_package( array( 'source' => $working_dir, 'destination' => WP_PLUGIN_DIR, 'clear_destination' => false, 'abort_if_destination_exists' => false, 'clear_working' => true, 'hook_extra' => array( 'type' => 'plugin', 'action' => 'install', ), ) ); if ( is_wp_error( $result ) ) { throw new Exception( $result->get_error_message() ); } $activate = true; } catch ( Exception $e ) { $success = false; } // Discard feedback ob_end_clean(); } wp_clean_plugins_cache(); // Activate this thing if ( $activate ) { try { $result = activate_plugin( $plugin ); if ( is_wp_error( $result ) ) { $success = false; } else { $success = true; } } catch ( Exception $e ) { $success = false; } } } if ( $success && function_exists( 'ayecode_connect_args' ) ) { ayecode_connect();// init $args = ayecode_connect_args(); $client = new AyeCode_Connect( $args ); $redirect_to = ! empty( $_POST['redirect_to'] ) ? esc_url_raw( $_POST['redirect_to'] ) : ''; $redirect = $client->build_connect_url( $redirect_to ); wp_send_json_success( array( 'connect_url' => $redirect ) ); } else { wp_send_json_error( $this->strings['error_localhost'] ); } wp_die(); } /** * Check if maybe localhost. * * @return bool */ public function is_localhost() { $localhost = false; $host = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : ''; $localhost_domains = array( 'localhost', 'localhost.localdomain', '127.0.0.1', '::1' ); if ( in_array( $host, $localhost_domains ) ) { $localhost = true; } return $localhost; } /** * Show notice to connect site. */ public function ayecode_connect_install_notice() { if ( $this->maybe_show() ) { $connect_title_string = $this->strings['connect_title']; $connect_external_string = $this->strings['connect_external']; $connect_string = $this->strings['connect']; $connect_button_string = $this->strings['connect_button']; $connecting_button_string = $this->strings['connecting_button']; ?>