| 1 |
<?php |
| 2 |
/** |
| 3 |
* Settings Wizard class. |
| 4 |
* |
| 5 |
* @package WebberZone\Top_Ten\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WebberZone\Top_Ten\Admin; |
| 9 |
|
| 10 |
use WebberZone\Top_Ten\Admin\Settings\Settings_Wizard_API; |
| 11 |
use WebberZone\Top_Ten\Admin\Settings; |
| 12 |
use function WebberZone\Top_Ten\wz_top_ten; |
| 13 |
|
| 14 |
// If this file is called directly, abort. |
| 15 |
if ( ! defined( 'WPINC' ) ) { |
| 16 |
die; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Settings Wizard class for Top 10. |
| 21 |
* |
| 22 |
* @since 4.2.0 |
| 23 |
*/ |
| 24 |
class Settings_Wizard extends Settings_Wizard_API { |
| 25 |
|
| 26 |
/** |
| 27 |
* Main constructor class. |
| 28 |
* |
| 29 |
* @since 4.2.0 |
| 30 |
*/ |
| 31 |
public function __construct() { |
| 32 |
$settings_key = 'tptn_settings'; |
| 33 |
$prefix = 'tptn'; |
| 34 |
|
| 35 |
$args = array( |
| 36 |
'steps' => $this->get_wizard_steps(), |
| 37 |
'translation_strings' => $this->get_translation_strings(), |
| 38 |
'page_slug' => 'tptn_wizard', |
| 39 |
'menu_args' => array( |
| 40 |
'parent' => 'tptn_options_page', |
| 41 |
'capability' => 'manage_options', |
| 42 |
), |
| 43 |
); |
| 44 |
|
| 45 |
parent::__construct( $settings_key, $prefix, $args ); |
| 46 |
$this->additional_hooks(); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Use the plugin version for cache-busting wizard assets. |
| 51 |
* |
| 52 |
* @since 4.2.0 |
| 53 |
* |
| 54 |
* @return string Version number. |
| 55 |
*/ |
| 56 |
protected function get_version() { |
| 57 |
return TOP_TEN_VERSION; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Additional hooks specific to Top 10. |
| 62 |
* |
| 63 |
* @since 4.2.0 |
| 64 |
*/ |
| 65 |
protected function additional_hooks() { |
| 66 |
// Trigger wizard setup logic on plugin activation. |
| 67 |
add_action( 'tptn_activate', array( $this, 'trigger_wizard_on_activation' ) ); |
| 68 |
|
| 69 |
// Register Tom Select AJAX handlers for wizard taxonomy fields. |
| 70 |
add_action( 'wp_ajax_nopriv_' . $this->prefix . '_taxonomy_search_tom_select', array( Settings::class, 'taxonomy_search_tom_select' ) ); |
| 71 |
add_action( 'wp_ajax_' . $this->prefix . '_taxonomy_search_tom_select', array( Settings::class, 'taxonomy_search_tom_select' ) ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Get wizard steps configuration. |
| 76 |
* |
| 77 |
* @since 4.2.0 |
| 78 |
* |
| 79 |
* @return array Wizard steps. |
| 80 |
*/ |
| 81 |
public function get_wizard_steps() { |
| 82 |
$all_settings_grouped = Settings::get_registered_settings(); |
| 83 |
$all_settings = array(); |
| 84 |
foreach ( $all_settings_grouped as $section_settings ) { |
| 85 |
$all_settings = array_merge( $all_settings, $section_settings ); |
| 86 |
} |
| 87 |
|
| 88 |
$basic_settings_keys = array( |
| 89 |
'add_to', |
| 90 |
'limit', |
| 91 |
'post_types', |
| 92 |
'range_desc', |
| 93 |
'daily_range', |
| 94 |
'hour_range', |
| 95 |
); |
| 96 |
|
| 97 |
$display_settings_keys = array( |
| 98 |
'title', |
| 99 |
'title_daily', |
| 100 |
'show_excerpt', |
| 101 |
'show_author', |
| 102 |
'show_date', |
| 103 |
'post_thumb_op', |
| 104 |
'thumb_size', |
| 105 |
); |
| 106 |
|
| 107 |
$content_tuning_keys = array( |
| 108 |
'exclude_front', |
| 109 |
'exclude_post_ids', |
| 110 |
'exclude_cat_slugs', |
| 111 |
'exclude_on_cat_slugs', |
| 112 |
); |
| 113 |
|
| 114 |
$admin_settings_keys = array( |
| 115 |
'show_metabox', |
| 116 |
'show_metabox_admins', |
| 117 |
'pv_in_admin', |
| 118 |
'show_count_non_admins', |
| 119 |
); |
| 120 |
|
| 121 |
$pro_settings_keys = array( |
| 122 |
'use_global_settings', |
| 123 |
'admin_column_post_types', |
| 124 |
'show_dashboard_to_roles', |
| 125 |
'max_execution_time', |
| 126 |
); |
| 127 |
|
| 128 |
$steps = array( |
| 129 |
'welcome' => array( |
| 130 |
'title' => __( 'Welcome to Top 10', 'top-10' ), |
| 131 |
'description' => __( 'Thank you for installing Top 10! This wizard will help you configure the essential settings to get your popular posts list working perfectly.', 'top-10' ), |
| 132 |
'settings' => array(), |
| 133 |
), |
| 134 |
'basic_settings' => array( |
| 135 |
'title' => __( 'Basic Settings', 'top-10' ), |
| 136 |
'description' => __( 'Configure how Top 10 tracks and counts your popular posts.', 'top-10' ), |
| 137 |
'settings' => $this->build_step_settings( $basic_settings_keys, $all_settings ), |
| 138 |
), |
| 139 |
'display_options' => array( |
| 140 |
'title' => __( 'Display Options', 'top-10' ), |
| 141 |
'description' => __( 'Customize how your popular posts list will look and what information to display.', 'top-10' ), |
| 142 |
'settings' => $this->build_step_settings( $display_settings_keys, $all_settings ), |
| 143 |
), |
| 144 |
'content_tuning' => array( |
| 145 |
'title' => __( 'Content Tuning', 'top-10' ), |
| 146 |
'description' => __( 'Fine-tune which content is included and how posts are excluded from the list.', 'top-10' ), |
| 147 |
'settings' => $this->build_step_settings( $content_tuning_keys, $all_settings ), |
| 148 |
), |
| 149 |
'admin_settings' => array( |
| 150 |
'title' => __( 'Admin Settings', 'top-10' ), |
| 151 |
'description' => __( 'Configure how Top 10 integrates with your admin area, dashboards, and user roles.', 'top-10' ), |
| 152 |
'settings' => $this->build_step_settings( $admin_settings_keys, $all_settings ), |
| 153 |
), |
| 154 |
'pro_settings' => array( |
| 155 |
'title' => __( 'Pro Settings', 'top-10' ), |
| 156 |
'description' => __( 'Configure Pro-only options such as global block settings, category exclusions, and query optimisation. These features require Top 10 Pro.', 'top-10' ), |
| 157 |
'settings' => $this->build_step_settings( $pro_settings_keys, $all_settings ), |
| 158 |
), |
| 159 |
); |
| 160 |
|
| 161 |
/** |
| 162 |
* Filter wizard steps. |
| 163 |
* |
| 164 |
* @param array $steps Wizard steps. |
| 165 |
*/ |
| 166 |
return apply_filters( 'tptn_wizard_steps', $steps ); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Build settings array for a wizard step from keys. |
| 171 |
* |
| 172 |
* @since 4.2.0 |
| 173 |
* |
| 174 |
* @param array $keys Setting keys for this step. |
| 175 |
* @param array $all_settings All settings array. |
| 176 |
* @return array |
| 177 |
*/ |
| 178 |
protected function build_step_settings( $keys, $all_settings ) { |
| 179 |
$step_settings = array(); |
| 180 |
|
| 181 |
foreach ( $keys as $key ) { |
| 182 |
if ( isset( $all_settings[ $key ] ) ) { |
| 183 |
$step_settings[ $key ] = $all_settings[ $key ]; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return $step_settings; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Get translation strings for the wizard. |
| 192 |
* |
| 193 |
* @since 4.2.0 |
| 194 |
* |
| 195 |
* @return array Translation strings. |
| 196 |
*/ |
| 197 |
public function get_translation_strings() { |
| 198 |
return array( |
| 199 |
'page_title' => __( 'Top 10 Setup Wizard', 'top-10' ), |
| 200 |
'menu_title' => __( 'Setup Wizard', 'top-10' ), |
| 201 |
'next_step' => __( 'Next Step', 'top-10' ), |
| 202 |
'previous_step' => __( 'Previous Step', 'top-10' ), |
| 203 |
'finish_setup' => __( 'Finish Setup', 'top-10' ), |
| 204 |
'skip_wizard' => __( 'Skip Wizard', 'top-10' ), |
| 205 |
/* translators: %s: Search query. */ |
| 206 |
'tom_select_no_results' => __( 'No results found for "%s"', 'top-10' ), |
| 207 |
'steps_nav_aria_label' => __( 'Setup Wizard Steps', 'top-10' ), |
| 208 |
/* translators: %1$d: Current step number, %2$d: Total number of steps */ |
| 209 |
'step_of' => __( 'Step %1$d of %2$d', 'top-10' ), |
| 210 |
'wizard_complete' => __( 'Setup Complete!', 'top-10' ), |
| 211 |
'setup_complete' => __( 'Your Top 10 plugin has been configured successfully. You can now start displaying popular posts on your site!', 'top-10' ), |
| 212 |
'go_to_settings' => __( 'Go to Settings', 'top-10' ), |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Trigger wizard on plugin activation. |
| 218 |
* |
| 219 |
* @since 4.2.0 |
| 220 |
*/ |
| 221 |
public function trigger_wizard_on_activation() { |
| 222 |
// Set a transient that will trigger the wizard on first admin page visit. |
| 223 |
// This works better than an option because it's temporary and won't persist |
| 224 |
// if the wizard is never accessed. |
| 225 |
set_transient( 'tptn_show_wizard_activation_redirect', true, HOUR_IN_SECONDS ); |
| 226 |
|
| 227 |
// Also set an option for more persistent storage in multisite environments. |
| 228 |
update_option( 'tptn_show_wizard', true ); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Get the URL to redirect to after wizard completion. |
| 233 |
* |
| 234 |
* @since 4.2.0 |
| 235 |
* |
| 236 |
* @return string Redirect URL. |
| 237 |
*/ |
| 238 |
protected function get_completion_redirect_url() { |
| 239 |
return admin_url( 'admin.php?page=tptn_options_page' ); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Override render_wizard_page to handle custom steps. |
| 244 |
* |
| 245 |
* @since 4.2.0 |
| 246 |
*/ |
| 247 |
public function render_wizard_page() { |
| 248 |
$this->current_step = $this->get_current_step(); |
| 249 |
$step_config = $this->get_current_step_config(); |
| 250 |
|
| 251 |
if ( empty( $step_config ) ) { |
| 252 |
$this->render_completion_page(); |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
// Check if this is a custom step. |
| 257 |
if ( ! empty( $step_config['custom_step'] ) ) { |
| 258 |
$this->render_custom_tables_step( $step_config ); |
| 259 |
return; |
| 260 |
} |
| 261 |
|
| 262 |
// Use parent method for regular steps. |
| 263 |
parent::render_wizard_page(); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Render the custom tables indexing step. |
| 268 |
* |
| 269 |
* @since 4.2.0 |
| 270 |
* |
| 271 |
* @param array $step_config Step configuration. |
| 272 |
*/ |
| 273 |
protected function render_custom_tables_step( $step_config ) { |
| 274 |
?> |
| 275 |
<div class="wrap wizard-wrap"> |
| 276 |
<h1><?php echo esc_html( $this->translation_strings['wizard_title'] ); ?></h1> |
| 277 |
|
| 278 |
<?php $this->render_wizard_steps_navigation(); ?> |
| 279 |
|
| 280 |
<div class="wizard-progress"> |
| 281 |
<div class="wizard-progress-bar"> |
| 282 |
<div class="wizard-progress-fill" style="width: <?php echo esc_attr( (string) ( ( $this->current_step / $this->total_steps ) * 100 ) ); ?>%;"></div> |
| 283 |
</div> |
| 284 |
<p class="wizard-step-counter"> |
| 285 |
<?php |
| 286 |
printf( |
| 287 |
esc_html( $this->translation_strings['step_of'] ), |
| 288 |
esc_html( (string) $this->current_step ), |
| 289 |
esc_html( (string) $this->total_steps ) |
| 290 |
); |
| 291 |
?> |
| 292 |
</p> |
| 293 |
</div> |
| 294 |
|
| 295 |
<div class="wizard-content"> |
| 296 |
<div class="wizard-step"> |
| 297 |
<h2><?php echo esc_html( $step_config['title'] ?? '' ); ?></h2> |
| 298 |
|
| 299 |
<?php if ( ! empty( $step_config['description'] ) ) : ?> |
| 300 |
<p class="wizard-step-description"><?php echo wp_kses_post( $step_config['description'] ); ?></p> |
| 301 |
<?php endif; ?> |
| 302 |
|
| 303 |
<form method="post" action=""> |
| 304 |
<?php wp_nonce_field( "{$this->prefix}_wizard_nonce", "{$this->prefix}_wizard_nonce" ); ?> |
| 305 |
|
| 306 |
<div class="wizard-fields"> |
| 307 |
<?php $this->render_custom_tables_interface(); ?> |
| 308 |
</div> |
| 309 |
|
| 310 |
<div class="wizard-actions"> |
| 311 |
<?php $this->render_wizard_buttons(); ?> |
| 312 |
</div> |
| 313 |
</form> |
| 314 |
</div> |
| 315 |
</div> |
| 316 |
</div> |
| 317 |
<?php |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Render the custom tables indexing interface. |
| 322 |
* |
| 323 |
* @since 4.2.0 |
| 324 |
*/ |
| 325 |
protected function render_custom_tables_interface() { |
| 326 |
// Top 10 does not implement the CRP custom tables UI in its wizard. |
| 327 |
// Intentionally left blank. |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Override the render completion page to show Top 10 specific content. |
| 332 |
* |
| 333 |
* @since 4.2.0 |
| 334 |
*/ |
| 335 |
protected function render_completion_page() { |
| 336 |
?> |
| 337 |
<div class="wrap wizard-wrap wizard-complete"> |
| 338 |
<div class="wizard-completion-header"> |
| 339 |
<h1><?php echo esc_html( $this->translation_strings['wizard_complete'] ); ?></h1> |
| 340 |
<p class="wizard-completion-message"> |
| 341 |
<?php echo esc_html( $this->translation_strings['setup_complete'] ); ?> |
| 342 |
</p> |
| 343 |
</div> |
| 344 |
|
| 345 |
<div class="wizard-completion-content"> |
| 346 |
<div class="wizard-completion-actions"> |
| 347 |
<a href="<?php echo esc_url( $this->get_completion_redirect_url() ); ?>" class="button button-primary button-large"> |
| 348 |
<?php esc_html_e( 'Go to Settings', 'top-10' ); ?> |
| 349 |
</a> |
| 350 |
<a href="<?php echo esc_url( admin_url( 'admin.php?page=tptn_tools_page' ) ); ?>" class="button button-secondary button-large"> |
| 351 |
<?php esc_html_e( 'Go to Tools', 'top-10' ); ?> |
| 352 |
</a> |
| 353 |
<a href="<?php echo esc_url( home_url( '/' ) ); ?>" class="button button-secondary button-large" target="_blank" rel="noopener noreferrer"> |
| 354 |
<?php esc_html_e( 'View Site', 'top-10' ); ?> |
| 355 |
</a> |
| 356 |
</div> |
| 357 |
</div> |
| 358 |
</div> |
| 359 |
<?php |
| 360 |
} |
| 361 |
} |
| 362 |
|