| 1 |
<?php |
| 2 |
/** |
| 3 |
* WpStream Onboarding Wizard |
| 4 |
*/ |
| 5 |
|
| 6 |
// Exit if accessed directly. |
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* |
| 13 |
* Class WpStream_Dashboard_Widget |
| 14 |
|
| 15 |
*/ |
| 16 |
|
| 17 |
class WpStream_Onboarding_Wizard { |
| 18 |
|
| 19 |
|
| 20 |
/** |
| 21 |
* |
| 22 |
* WpStream_Onboarding_Wizard constructor. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
public function __construct() { |
| 27 |
|
| 28 |
add_action( 'admin_init', array( $this, 'wpstream_load_onboarding_wizard' ) ); |
| 29 |
|
| 30 |
add_action( 'admin_menu', array( $this, 'add_dashboard_page' ) ); |
| 31 |
add_action( 'network_admin_menu', array( $this, 'add_dashboard_page' ) ); |
| 32 |
|
| 33 |
|
| 34 |
add_action( 'wp_ajax_WpStream_onboarding_get_errors', array( |
| 35 |
$this, |
| 36 |
'get_install_errors', |
| 37 |
) ); |
| 38 |
|
| 39 |
// This will only be called in the Onboarding Wizard context because of previous checks. |
| 40 |
add_filter( 'WpStream_maybe_authenticate_siteurl', array( $this, 'change_return_url' ) ); |
| 41 |
add_filter( 'WpStream_auth_success_redirect_url', array( $this, 'change_success_url' ) ); |
| 42 |
add_filter( 'WpStream_reauth_success_redirect_url', array( $this, 'change_success_url' ) ); |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* |
| 53 |
* Checks if the Wizard should be loaded in current context. |
| 54 |
* |
| 55 |
*/ |
| 56 |
public function wpstream_load_onboarding_wizard() { |
| 57 |
|
| 58 |
// Check for wizard-specific parameter |
| 59 |
// Allow plugins to disable the onboarding wizard |
| 60 |
// Check if current user is allowed to save settings. |
| 61 |
|
| 62 |
if ( ! ( isset( $_GET['page'] ) |
| 63 |
|| 'WpStream-onboarding' !== $_GET['page'] |
| 64 |
// || apply_filters( 'WpStream_enable_onboarding_wizard', true ) |
| 65 |
//|| ! current_user_can( 'WpStream_save_settings' ) |
| 66 |
) ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
// Don't load the interface if doing an ajax call. |
| 71 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
set_current_screen(); |
| 76 |
|
| 77 |
// Remove an action in the Gutenberg plugin ( not core Gutenberg ) which throws an error. |
| 78 |
remove_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' ); |
| 79 |
|
| 80 |
$this->actual_load_onboarding_wizard(); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* |
| 89 |
* Register page through WordPress's hooks. |
| 90 |
* |
| 91 |
*/ |
| 92 |
public function add_dashboard_page() { |
| 93 |
add_dashboard_page( 'Wpstream x', 'WpStream Menu x', 'administrator', 'wpstream-onboarding', '' ); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
/* |
| 100 |
* Load the Onboarding Wizard file |
| 101 |
* |
| 102 |
*/ |
| 103 |
|
| 104 |
private function actual_load_onboarding_wizard() { |
| 105 |
|
| 106 |
$this->wpstream_load_css_js(); |
| 107 |
$this->onboarding_wizard_header(); |
| 108 |
$this->onboarding_wizard_content(); |
| 109 |
$this->onboarding_wizard_footer(); |
| 110 |
|
| 111 |
exit; |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
private function wpstream_load_css_js(){ |
| 117 |
wp_enqueue_style( 'wptream-on-boarding-css', plugin_dir_url( __FILE__ ) . 'css/wpstream-admin-onboarding.css', array(), WPSTREAM_PLUGIN_VERSION, 'all' ); |
| 118 |
|
| 119 |
wp_enqueue_script('wpstream-on-boarding-js', plugin_dir_url( __DIR__ ) .'/admin/js/wpstream-onboarding.js?v='.time(),array(), WPSTREAM_PLUGIN_VERSION, true); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
/* |
| 128 |
* |
| 129 |
* Outputs the simplified header used for the Onboarding Wizard. |
| 130 |
* |
| 131 |
*/ |
| 132 |
public function onboarding_wizard_header() { |
| 133 |
?> |
| 134 |
<!DOCTYPE html> |
| 135 |
<html <?php language_attributes(); ?>> |
| 136 |
<head> |
| 137 |
<meta name="viewport" content="width=device-width"/> |
| 138 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
| 139 |
<title><?php esc_html_e( 'WpStream › Onboarding Wizard', 'wpstream' ); ?></title> |
| 140 |
<?php do_action( 'admin_print_styles' ); ?> |
| 141 |
<?php do_action( 'admin_print_scripts' ); ?> |
| 142 |
<?php do_action( 'admin_head' ); ?> |
| 143 |
</head> |
| 144 |
<body class="WpStream-onboarding"> |
| 145 |
<?php |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
/* |
| 153 |
* |
| 154 |
* Outputs the content of the current step. |
| 155 |
* |
| 156 |
*/ |
| 157 |
public function onboarding_wizard_content() { |
| 158 |
$admin_url = is_network_admin() ? network_admin_url() : admin_url(); |
| 159 |
|
| 160 |
WpStream_settings_error_page( 'WpStream-onboarding-wizard', '<a href="' . $admin_url . '">' . esc_html__( 'Return to Dashboard', 'wpstream') . '</a>' ); |
| 161 |
} |
| 162 |
|
| 163 |
/* |
| 164 |
* |
| 165 |
* Wizzard Footer |
| 166 |
* |
| 167 |
*/ |
| 168 |
public function onboarding_wizard_footer() { |
| 169 |
?> |
| 170 |
|
| 171 |
</body> |
| 172 |
</html> |
| 173 |
<?php |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Update the redirect url so the user returns to the Onboarding Wizard after auth. |
| 180 |
* |
| 181 |
* @param string $siteurl The url to which the user is redirected for auth. |
| 182 |
* |
| 183 |
* |
| 184 |
*/ |
| 185 |
|
| 186 |
public function change_return_url( $siteurl ) { |
| 187 |
|
| 188 |
$url = wp_parse_url( $siteurl ); |
| 189 |
$admin_url = is_network_admin() ? network_admin_url() : admin_url(); |
| 190 |
|
| 191 |
if ( isset( $url['query'] ) ) { |
| 192 |
|
| 193 |
parse_str( $url['query'], $parameters ); |
| 194 |
|
| 195 |
$parameters['return'] = rawurlencode( add_query_arg( array( |
| 196 |
'page' => 'WpStream-onboarding', |
| 197 |
), $admin_url ) ); |
| 198 |
|
| 199 |
$siteurl = str_replace( $url['query'], '', $siteurl ); |
| 200 |
|
| 201 |
$siteurl = add_query_arg( $parameters, $siteurl ); |
| 202 |
|
| 203 |
$siteurl .= '#/authenticate'; |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
return $siteurl; |
| 208 |
|
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Update the success redirect URL so if all is well we get to the next step. |
| 213 |
* |
| 214 |
* @param string $siteurl The url to which the user is redirected after a successful auth. |
| 215 |
* |
| 216 |
* @return mixed |
| 217 |
*/ |
| 218 |
public function change_success_url( $siteurl ) { |
| 219 |
|
| 220 |
$admin_url = is_network_admin() ? network_admin_url() : admin_url(); |
| 221 |
$return_step = is_network_admin() ? 'recommended_addons' : 'recommended_settings'; |
| 222 |
|
| 223 |
$siteurl = add_query_arg( array( |
| 224 |
'page' => 'WpStream-onboarding', |
| 225 |
), $admin_url ); |
| 226 |
|
| 227 |
$siteurl .= '#/' . $return_step; |
| 228 |
|
| 229 |
return $siteurl; |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
/** |
| 235 |
* Ajax handler for grabbing the installed code status. |
| 236 |
*/ |
| 237 |
public function get_install_errors() { |
| 238 |
|
| 239 |
wp_send_json( WpStream_is_code_installed_frontend() ); |
| 240 |
|
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
//new WpStream_Onboarding_Wizard(); |
| 249 |
|