'fusewp/fusewp.php', 'lite_download_url' => 'https://downloads.wordpress.org/plugin/fusewp.latest-stable.zip', 'fusewp_settings' => 'admin.php?page=fusewp-sync', ); private $output_data = array(); public function __construct() { if ( ! $this->is_activated()) { add_action('admin_menu', array($this, 'register_settings_page')); } add_action('wp_ajax_pabc_activate_plugin', [$this, 'pabc_activate_plugin']); add_action('wp_ajax_pabc_install_plugin', [$this, 'pabc_install_plugin']); if (wp_doing_ajax()) { add_action('wp_ajax_pabc_fusewp_page_check_plugin_status', array($this, 'ajax_check_plugin_status')); } // Check what page we are on. $page = isset($_GET['page']) ? sanitize_key(wp_unslash($_GET['page'])) : ''; if (self::SLUG !== $page) return; add_action('admin_init', array($this, 'redirect_to_fusewp_settings')); add_action('admin_enqueue_scripts', array($this, 'enqueue_assets')); } public function pabc_install_plugin() { // Run a security check. check_ajax_referer('pabc-admin-nonce', 'nonce'); $generic_error = esc_html__('There was an error while performing your request.', 'wp-user-avatar'); $type = ! empty($_POST['type']) ? sanitize_key($_POST['type']) : 'plugin'; if ( ! current_user_can('install_plugins')) { wp_send_json_error($generic_error); } // Determine whether file modifications are allowed. if ( ! wp_is_file_mod_allowed('pabc_can_install')) { wp_send_json_error($generic_error); } $error = $type === 'plugin' ? esc_html__('Could not install plugin. Please download and install manually.', 'wp-user-avatar') : esc_html__('Could not install addon. Please download from wpforms.com and install manually.', 'wp-user-avatar'); if (empty($_POST['plugin'])) { wp_send_json_error($error); } // Set the current screen to avoid undefined notices. set_current_screen('pabc_page_pabc-fusewp'); // Prepare variables. $url = esc_url_raw( add_query_arg( ['page' => 'pabc-settings'], admin_url('admin.php') ) ); ob_start(); $creds = request_filesystem_credentials($url, '', false, false, null); // Hide the filesystem credentials form. ob_end_clean(); // Check for file system permissions. if ($creds === false) { wp_send_json_error($error); } if ( ! WP_Filesystem($creds)) { wp_send_json_error($error); } /* * We do not need any extra credentials if we have gotten this far, so let's install the plugin. */ // Do not allow WordPress to search/download translations, as this will break JS output. remove_action('upgrader_process_complete', ['Language_Pack_Upgrader', 'async_upgrade'], 20); // Create the plugin upgrader with our custom skin. $installer = new PABC_PluginSilentUpgrader(new PABC_Install_Skin()); // Error check. if ( ! method_exists($installer, 'install') || empty($_POST['plugin'])) { wp_send_json_error($error); } $installer->install($_POST['plugin']); // phpcs:ignore // Flush the cache and return the newly installed plugin basename. wp_cache_flush(); $plugin_basename = $installer->plugin_info(); if (empty($plugin_basename)) { wp_send_json_error($error); } $result = [ 'msg' => $generic_error, 'is_activated' => false, 'basename' => $plugin_basename, ]; // Check for permissions. if ( ! current_user_can('activate_plugins')) { $result['msg'] = $type === 'plugin' ? esc_html__('Plugin installed.', 'wp-user-avatar') : esc_html__('Addon installed.', 'wp-user-avatar'); wp_send_json_success($result); } // Activate the plugin silently. $activated = activate_plugin($plugin_basename); if ( ! is_wp_error($activated)) { $result['is_activated'] = true; $result['msg'] = $type === 'plugin' ? esc_html__('Plugin installed & activated.', 'wp-user-avatar') : esc_html__('Addon installed & activated.', 'wp-user-avatar'); wp_send_json_success($result); } // Fallback error just in case. wp_send_json_error($result); } public function pabc_activate_plugin() { // Run a security check. check_ajax_referer('pabc-admin-nonce', 'nonce'); // Check for permissions. if ( ! current_user_can('activate_plugins')) { wp_send_json_error(esc_html__('Plugin activation is disabled for you on this site.', 'wp-user-avatar')); } if (isset($_POST['plugin'])) { $plugin = sanitize_text_field(wp_unslash($_POST['plugin'])); $activate = activate_plugins($plugin); if ( ! is_wp_error($activate)) { wp_send_json_success(esc_html__('Plugin activated.', 'wp-user-avatar')); } } wp_send_json_error(esc_html__('Could not activate plugin. Please activate from the Plugins page.', 'wp-user-avatar')); } public function register_settings_page() { $newIndicator = sprintf( ' %s', __('NEW', 'wp-user-avatar') ); add_submenu_page( 'admin-bar-dashboard-control', 'User Sync', esc_html__('User Sync', 'wp-user-avatar') . $newIndicator, 'manage_options', self::SLUG, array($this, 'output') ); } public function enqueue_assets() { wp_enqueue_script( 'pabc-admin-page-fusewp', PABC_ASSETS_URL . "fusewp.js", array('jquery'), false, true ); wp_localize_script( 'pabc-admin-page-fusewp', 'pabc_pluginlanding', $this->get_js_strings() ); wp_localize_script('pabc-admin-page-fusewp', 'pabc_installer_globals', [ 'nonce' => wp_create_nonce('pabc-admin-nonce') ]); } /** * JS Strings. */ protected function get_js_strings() { $error_could_not_install = sprintf( wp_kses( /* translators: %s - Lite plugin download URL. */ __('Could not install plugin. Please download and install manually.', 'wp-user-avatar'), array( 'a' => array( 'href' => true, ), ) ), esc_url_raw($this->config['lite_download_url']) ); $error_could_not_activate = sprintf( wp_kses( /* translators: %s - Lite plugin download URL. */ __('Could not activate plugin. Please activate from the Plugins page.', 'wp-user-avatar'), array( 'a' => array( 'href' => true, ), ) ), esc_url_raw(admin_url('plugins.php')) ); return array( 'installing' => esc_html__('Installing...', 'wp-user-avatar'), 'activating' => esc_html__('Activating...', 'wp-user-avatar'), 'activated' => esc_html__('FuseWP Installed & Activated', 'wp-user-avatar'), 'install_now' => esc_html__('Install Now', 'wp-user-avatar'), 'activate_now' => esc_html__('Activate Now', 'wp-user-avatar'), 'download_now' => esc_html__('Download Now', 'wp-user-avatar'), 'plugins_page' => esc_html__('Go to Plugins page', 'wp-user-avatar'), 'error_could_not_install' => $error_could_not_install, 'error_could_not_activate' => $error_could_not_activate, 'manual_install_url' => $this->config['lite_download_url'], 'manual_activate_url' => admin_url('plugins.php'), 'fusewp_settings_button' => esc_html__('Go to FuseWP Settings', 'wp-user-avatar'), ); } /** * Generate and output page HTML. */ public function output() { ?> '; $this->output_section_heading(); $this->output_section_screenshot(); $this->output_section_step_install(); $this->output_section_step_setup(); echo ''; } /** * Generate and output heading section HTML. */ protected function output_section_heading() { // Heading section. printf( '

%1$s

%2$s

', esc_html__('WordPress User Sync & Automation Plugin', 'wp-user-avatar'), esc_html__('FuseWP connect WordPress to your email marketing platform and CRM so you can automatically sync users & profile updates to your email list.', 'wp-user-avatar') ); } /** * Generate and output screenshot section HTML. */ protected function output_section_screenshot() { printf( '
%2$s
', PABC_ASSETS_URL . 'fusewp-user-sync-edit-screen.png', esc_attr__('FuseWP screenshot', 'wp-user-avatar') ); } /** * Generate and output step 'Install' section HTML. */ protected function output_section_step_install() { $step = $this->get_data_step_install(); if (empty($step)) { return; } printf( '

%3$s

%4$s

', esc_url(PABC_ASSETS_URL . $step['icon']), esc_attr__('Step 1', 'wp-user-avatar'), esc_html__('Install and Activate FuseWP', 'wp-user-avatar'), esc_html__('Install FuseWP from the WordPress.org plugin repository.', 'wp-user-avatar'), esc_attr($step['button_class']), esc_attr($step['plugin']), esc_attr($step['button_action']), esc_html($step['button_text']) ); } /** * Generate and output step 'Setup' section HTML. */ protected function output_section_step_setup() { $step = $this->get_data_step_setup(); if (empty($step)) { return; } printf( '

%4$s

%5$s

', esc_attr($step['section_class']), esc_url(PABC_ASSETS_URL . $step['icon']), esc_attr__('Step 2', 'wp-user-avatar'), esc_html__('Set Up FuseWP', 'wp-user-avatar'), esc_html__('Configure and create your first sync rule.', 'wp-user-avatar'), esc_attr($step['button_class']), esc_url(admin_url($this->config['fusewp_settings'])), esc_html($step['button_text']) ); } /** * Step 'Install' data. */ protected function get_data_step_install() { $step = array(); $this->output_data['all_plugins'] = get_plugins(); $this->output_data['plugin_installed'] = array_key_exists($this->config['lite_plugin'], $this->output_data['all_plugins']); $this->output_data['plugin_activated'] = false; $this->output_data['plugin_setup'] = false; if ( ! $this->output_data['plugin_installed']) { $step['icon'] = 'step-1.svg'; $step['button_text'] = esc_html__('Install FuseWP', 'wp-user-avatar'); $step['button_class'] = ''; $step['button_action'] = 'install'; $step['plugin'] = $this->config['lite_download_url']; } else { $this->output_data['plugin_activated'] = $this->is_activated(); $this->output_data['plugin_setup'] = $this->is_configured(); $step['icon'] = $this->output_data['plugin_activated'] ? 'step-complete.svg' : 'step-1.svg'; $step['button_text'] = $this->output_data['plugin_activated'] ? esc_html__('FuseWP Installed & Activated', 'wp-user-avatar') : esc_html__('Activate FuseWP', 'wp-user-avatar'); $step['button_class'] = $this->output_data['plugin_activated'] ? 'grey disabled' : ''; $step['button_action'] = $this->output_data['plugin_activated'] ? '' : 'activate'; $step['plugin'] = $this->config['lite_plugin']; } return $step; } /** * Step 'Setup' data. */ protected function get_data_step_setup() { $step = array(); $step['icon'] = 'step-2.svg'; $step['section_class'] = $this->output_data['plugin_activated'] ? '' : 'grey'; $step['button_text'] = esc_html__('Start Setup', 'wp-user-avatar'); $step['button_class'] = 'grey disabled'; if ($this->output_data['plugin_setup']) { $step['icon'] = 'step-complete.svg'; $step['section_class'] = ''; $step['button_text'] = esc_html__('Go to FuseWP settings', 'wp-user-avatar'); } else { $step['button_class'] = $this->output_data['plugin_activated'] ? '' : 'grey disabled'; } return $step; } /** * Ajax endpoint. Check plugin setup status. * Used to properly init step 'Setup' section after completing step 'Install'. */ public function ajax_check_plugin_status() { // Security checks. if ( ! check_ajax_referer('pabc-admin-nonce', 'nonce', false) || ! current_user_can('activate_plugins') ) { wp_send_json_error( array( 'error' => esc_html__('You do not have permission.', 'wp-user-avatar'), ) ); } $result = array(); if ( ! $this->is_activated()) { wp_send_json_error( array( 'error' => esc_html__('Plugin unavailable.', 'wp-user-avatar'), ) ); } $result['setup_status'] = (int)$this->is_configured(); wp_send_json_success($result); } /** * Whether FuseWP plugin configured or not. */ protected function is_configured() { return $this->is_activated(); } /** * Whether FuseWP plugin active or not. */ protected function is_activated() { return class_exists('\FuseWP\Core\Base'); } public function redirect_to_fusewp_settings() { if ($this->is_configured()) { wp_safe_redirect(admin_url($this->config['fusewp_settings'])); exit; } } /** * @return self */ public static function get_instance() { static $instance = null; if (is_null($instance)) { $instance = new self(); } return $instance; } }