| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package Tag Groups |
| 5 |
* @author Christoph Amthor |
| 6 |
* @copyright 2018 Christoph Amthor (@ Chatty Mango, chattymango.com) |
| 7 |
* @license GPL-3.0+ |
| 8 |
*/ |
| 9 |
|
| 10 |
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace, Squiz.Classes.ValidClassName.NotCamelCaps, PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 11 |
if (!class_exists('TagGroups_Setup_Wizard')) { |
| 12 |
/** |
| 13 |
* |
| 14 |
*/ |
| 15 |
class TagGroups_Setup_Wizard extends TagGroups_Settings |
| 16 |
{ |
| 17 |
/** |
| 18 |
* renders a menu-less settings page: onboarding |
| 19 |
* |
| 20 |
* @param void |
| 21 |
* @return void |
| 22 |
*/ |
| 23 |
public static function settings_page_onboarding() |
| 24 |
{ |
| 25 |
// Make very sure that only administrators can access this page |
| 26 |
if (!current_user_can('manage_options')) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
self::add_header(); |
| 31 |
$settings_taxonomy_link = admin_url('admin.php?page=tag-groups-settings'); |
| 32 |
$settings_home_link = admin_url('admin.php?page=tag-groups-settings'); |
| 33 |
$settings_premium_link = admin_url('admin.php?page=tag-groups-settings-premium'); |
| 34 |
$settings_setup_wizard_link = admin_url('admin.php?page=tag-groups-settings-setup-wizard'); |
| 35 |
|
| 36 |
if (defined('TAG_GROUPS_PLUGIN_IS_FREE') && TAG_GROUPS_PLUGIN_IS_FREE) { |
| 37 |
$title = 'Tag Groups'; |
| 38 |
$documentation_link = 'https://taxopress.com/docs/tag-groups/'; |
| 39 |
$logo = ''; |
| 40 |
} else { |
| 41 |
$title = 'Tag Groups Pro'; |
| 42 |
$documentation_link = 'https://taxopress.com/docs/tag-groups/'; |
| 43 |
$logo = ''; |
| 44 |
} |
| 45 |
|
| 46 |
$view = new TagGroups_View('admin/onboarding'); |
| 47 |
$view->set(array( |
| 48 |
'logo' => $logo, |
| 49 |
'title' => $title, |
| 50 |
'settings_taxonomy_link' => $settings_taxonomy_link, |
| 51 |
'settings_home_link' => $settings_home_link, |
| 52 |
'documentation_link' => $documentation_link, |
| 53 |
'settings_premium_link' => $settings_premium_link, |
| 54 |
'settings_setup_wizard_link' => $settings_setup_wizard_link, |
| 55 |
)); |
| 56 |
$view->render(); |
| 57 |
self::add_footer(); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* renders a menu-less settings page: onboarding |
| 62 |
* |
| 63 |
* @param void |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public static function settings_page_setup_wizard() |
| 67 |
{ |
| 68 |
// Make very sure that only administrators can access this page |
| 69 |
if (!current_user_can('manage_options')) { |
| 70 |
return; |
| 71 |
} |
| 72 |
global $tag_group_groups ; |
| 73 |
self::add_header(); |
| 74 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only wizard step navigation |
| 75 |
$step = ( isset($_GET['step']) && $_GET['step'] > 0 ? (int) $_GET['step'] : 1 ); |
| 76 |
$setup_wizard_next_link = add_query_arg('step', $step + 1, admin_url('admin.php?page=tag-groups-settings-setup-wizard')); |
| 77 |
|
| 78 |
if (defined('TAG_GROUPS_PLUGIN_IS_FREE') && TAG_GROUPS_PLUGIN_IS_FREE) { |
| 79 |
$title = 'Tag Groups'; |
| 80 |
$is_premium = false; |
| 81 |
$documentation_link = 'https://taxopress.com/docs/tag-groups/'; |
| 82 |
} else { |
| 83 |
$title = 'Tag Groups Pro'; |
| 84 |
$is_premium = true; |
| 85 |
$documentation_link = 'https://taxopress.com/docs/tag-groups/'; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
if ($is_premium && TagGroups_Utilities::is_premium_plan() && class_exists('TagGroups_Premium_View')) { |
| 90 |
} else { |
| 91 |
$steps = array( |
| 92 |
1 => array( |
| 93 |
'id' => 'start', |
| 94 |
'title' => __('Start', 'tag-groups'), |
| 95 |
), |
| 96 |
2 => array( |
| 97 |
'id' => 'taxonomies', |
| 98 |
'title' => __('Taxonomies', 'tag-groups'), |
| 99 |
), |
| 100 |
3 => array( |
| 101 |
'id' => 'sample_content', |
| 102 |
'title' => __('Sample Content', 'tag-groups'), |
| 103 |
), |
| 104 |
4 => array( |
| 105 |
'id' => 'finished', |
| 106 |
'title' => null, |
| 107 |
), |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
$view = new TagGroups_View('admin/setup_wizard_header'); |
| 112 |
$view->set(array( |
| 113 |
'title' => $title, |
| 114 |
'step' => $step, |
| 115 |
'steps' => $steps, |
| 116 |
)); |
| 117 |
$view->render(); |
| 118 |
switch ($steps[$step]['id']) { |
| 119 |
case 'sample_content': |
| 120 |
$view = new TagGroups_View('admin/setup_wizard_sample_content'); |
| 121 |
$group_names = array( |
| 122 |
__('Sample Group A', 'tag-groups'), |
| 123 |
__('Sample Group B', 'tag-groups'), |
| 124 |
__('Sample Group C', 'tag-groups') |
| 125 |
); |
| 126 |
/** |
| 127 |
* Make sure they don't yet exist |
| 128 |
*/ |
| 129 |
$group_names = array_map(function ($original_name) { |
| 130 |
$tg_group = new TagGroups_Group(); |
| 131 |
$name = $original_name; |
| 132 |
$i = 0; |
| 133 |
while ($tg_group->find_by_label($name) !== false) { |
| 134 |
$i++; |
| 135 |
$name = $original_name . ' - ' . $i; |
| 136 |
} |
| 137 |
return $name; |
| 138 |
}, $group_names); |
| 139 |
$tag_names = array( |
| 140 |
__('First Sample Tag', 'tag-groups'), |
| 141 |
__('Second Sample Tag', 'tag-groups'), |
| 142 |
__('Third Sample Tag', 'tag-groups') |
| 143 |
); |
| 144 |
$enabled_taxonomies = TagGroups_Taxonomy::get_enabled_taxonomies(); |
| 145 |
$taxonomy = array_shift($enabled_taxonomies); |
| 146 |
/** |
| 147 |
* Make sure they don't yet exist |
| 148 |
*/ |
| 149 |
$tag_names = array_map(function ($original_name) use ($taxonomy) { |
| 150 |
$name = $original_name; |
| 151 |
$i = 0; |
| 152 |
while (get_term_by('name', $name, $taxonomy) !== false) { |
| 153 |
$i++; |
| 154 |
$name = $original_name . ' - ' . $i; |
| 155 |
} |
| 156 |
return $name; |
| 157 |
}, $tag_names); |
| 158 |
|
| 159 |
if (TagGroups_Gutenberg::is_gutenberg_active()) { |
| 160 |
$create_sample_page_label = __('Create a draft sample page with Gutenberg blocks.', 'tag-groups'); |
| 161 |
} else { |
| 162 |
$create_sample_page_label = __('Create a draft sample page with shortcodes.', 'tag-groups'); |
| 163 |
} |
| 164 |
|
| 165 |
$view->set(array( |
| 166 |
'title' => $title, |
| 167 |
'group_names' => $group_names, |
| 168 |
'tag_names' => $tag_names, |
| 169 |
'create_sample_page_label' => $create_sample_page_label, |
| 170 |
'setup_wizard_next_link' => $setup_wizard_next_link, |
| 171 |
)); |
| 172 |
break; |
| 173 |
case 'post_tags': |
| 174 |
break; |
| 175 |
case 'meta_box': |
| 176 |
break; |
| 177 |
case 'taxonomies': |
| 178 |
$view = new TagGroups_View('admin/setup_wizard_taxonomies'); |
| 179 |
$view->set(array( |
| 180 |
'title' => $title, |
| 181 |
'public_taxonomies' => TagGroups_Taxonomy::get_public_taxonomies(), |
| 182 |
'enabled_taxonomies' => TagGroups_Taxonomy::get_enabled_taxonomies(), |
| 183 |
'setup_wizard_next_link' => $setup_wizard_next_link, |
| 184 |
)); |
| 185 |
break; |
| 186 |
case 'finished': |
| 187 |
$view = new TagGroups_View('admin/setup_wizard_finished'); |
| 188 |
$documentation_link = ( $is_premium ? 'https://taxopress.com/docs/tag-groups/' : 'https://taxopress.com/docs/how-do-i-change-the-styling-of-the-post-tags-under-the-posts/' ); |
| 189 |
$enabled_taxonomies = TagGroups_Taxonomy::get_enabled_taxonomies(); |
| 190 |
$taxonomy = array_shift($enabled_taxonomies); |
| 191 |
$view->set(array( |
| 192 |
'groups_admin_link' => TagGroups_Taxonomy::get_tag_group_admin_url($taxonomy), |
| 193 |
'documentation_link' => $documentation_link, |
| 194 |
'settings_home_link' => admin_url('admin.php?page=tag-groups-settings'), |
| 195 |
'tag_group_sample_page_id' => TagGroups_Options::get_option('tag_group_sample_page_id', 0), |
| 196 |
)); |
| 197 |
break; |
| 198 |
case 'start': |
| 199 |
default: |
| 200 |
$view = new TagGroups_View('admin/setup_wizard_start'); |
| 201 |
$view->set(array( |
| 202 |
'title' => $title, |
| 203 |
'setup_wizard_next_link' => $setup_wizard_next_link, |
| 204 |
'is_premium' => $is_premium, |
| 205 |
)); |
| 206 |
break; |
| 207 |
} |
| 208 |
$view->render(); |
| 209 |
$view = new TagGroups_View('admin/setup_wizard_footer'); |
| 210 |
$view->render(); |
| 211 |
self::add_footer(); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Processes form submissions from the settings page |
| 216 |
* |
| 217 |
* @param void |
| 218 |
* @return void |
| 219 |
*/ |
| 220 |
public static function settings_page_actions_wizard() |
| 221 |
{ |
| 222 |
global $tag_group_groups ; |
| 223 |
if (empty($_REQUEST['tg_action_wizard'])) { |
| 224 |
return; |
| 225 |
} |
| 226 |
// Make very sure that only administrators can do actions |
| 227 |
if (!current_user_can('manage_options')) { |
| 228 |
wp_die("Capability check failed"); |
| 229 |
} |
| 230 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce verification |
| 231 |
if (!isset($_POST['tag-groups-setup-wizard-nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['tag-groups-setup-wizard-nonce'])), 'tag-groups-setup-wizard-nonce')) { |
| 232 |
die("Security check failed"); |
| 233 |
} |
| 234 |
$enabled_taxonomies = TagGroups_Taxonomy::get_enabled_taxonomies(); |
| 235 |
$taxonomy = array_shift($enabled_taxonomies); |
| 236 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized in switch statement |
| 237 |
switch (sanitize_key(wp_unslash($_REQUEST['tg_action_wizard']))) { |
| 238 |
case 'taxonomy': |
| 239 |
if (isset($_POST['taxonomies'])) { |
| 240 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized below |
| 241 |
$taxonomies = wp_unslash($_POST['taxonomies']); |
| 242 |
|
| 243 |
if (is_array($taxonomies)) { |
| 244 |
$taxonomies = array_map('sanitize_text_field', $taxonomies); |
| 245 |
} else { |
| 246 |
$taxonomies = array( 'post_tag' ); |
| 247 |
} |
| 248 |
} else { |
| 249 |
$taxonomies = array( 'post_tag' ); |
| 250 |
} |
| 251 |
|
| 252 |
$public_taxonomies = TagGroups_Taxonomy::get_public_taxonomies(); |
| 253 |
foreach ($taxonomies as $taxonomy_item) { |
| 254 |
if (!in_array($taxonomy_item, $public_taxonomies)) { |
| 255 |
return; |
| 256 |
} |
| 257 |
} |
| 258 |
TagGroups_Taxonomy::update_enabled($taxonomies); |
| 259 |
break; |
| 260 |
case 'sample-content': |
| 261 |
$created_groups = array(); |
| 262 |
/** |
| 263 |
* Create groups |
| 264 |
*/ |
| 265 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized below |
| 266 |
if (isset($_POST['tag-groups-create-sample-groups']) && wp_unslash($_POST['tag-groups-create-sample-groups'])) { |
| 267 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- Sanitized below |
| 268 |
foreach (wp_unslash($_POST['tag_groups_group_names']) as $group_name) { |
| 269 |
$tg_group = new TagGroups_Group(); |
| 270 |
$tg_group->create(sanitize_text_field($group_name)); |
| 271 |
$created_groups[] = $tg_group->get_group_id(); |
| 272 |
} |
| 273 |
} |
| 274 |
/** |
| 275 |
* Create tags |
| 276 |
*/ |
| 277 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized below |
| 278 |
if (isset($_POST['tag-groups-create-sample-tags']) && wp_unslash($_POST['tag-groups-create-sample-tags'])) { |
| 279 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- Sanitized below |
| 280 |
foreach (wp_unslash($_POST['tag_groups_tag_names']) as $tag_name) { |
| 281 |
$tag_name = sanitize_text_field($tag_name); |
| 282 |
|
| 283 |
if (!term_exists($tag_name, $taxonomy)) { |
| 284 |
$term_array = wp_insert_term($tag_name, $taxonomy); |
| 285 |
$tg_term = new TagGroups_Term($term_array['term_id']); |
| 286 |
|
| 287 |
if (empty($created_groups)) { |
| 288 |
$group_ids = $tag_group_groups->get_group_ids(); |
| 289 |
unset($group_ids[0]); |
| 290 |
} else { |
| 291 |
$group_ids = $created_groups; |
| 292 |
} |
| 293 |
|
| 294 |
// add one group |
| 295 |
$amount = 1; |
| 296 |
|
| 297 |
if (1 == $amount) { |
| 298 |
$random_group_ids = $group_ids[array_rand($group_ids)]; |
| 299 |
} else { |
| 300 |
$random_group_ids = array_intersect_key($group_ids, array_rand($group_ids, $amount)); |
| 301 |
} |
| 302 |
|
| 303 |
$tg_term->add_group($random_group_ids)->save(); |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
$tpf_include = $tag_group_groups->get_group_ids(); |
| 308 |
unset($tpf_include[0]); |
| 309 |
|
| 310 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Checkbox validation |
| 311 |
if (isset($_POST['tag-groups-create-sample-page']) && wp_unslash($_POST['tag-groups-create-sample-page'])) { |
| 312 |
if (TagGroups_Gutenberg::is_gutenberg_active()) { |
| 313 |
$view = new TagGroups_View('admin/sample_page_gutenberg'); |
| 314 |
$sample_page_title = 'Tag Groups (Free) Sample Page - Gutenberg Editor'; |
| 315 |
} else { |
| 316 |
$view = new TagGroups_View('admin/sample_page'); |
| 317 |
$sample_page_title = 'Tag Groups (Free) Sample Page - Classic Editor'; |
| 318 |
} |
| 319 |
|
| 320 |
$tag_groups_settings_link = admin_url('admin.php?page=tag-groups-settings'); |
| 321 |
$current_user = wp_get_current_user(); |
| 322 |
$view->set(array( |
| 323 |
'enabled_taxonomies' => $enabled_taxonomies, |
| 324 |
'author_display_name' => $current_user->display_name, |
| 325 |
'tag_groups_settings_link' => $tag_groups_settings_link, |
| 326 |
'tpf_include_csv' => implode(',', $tpf_include), |
| 327 |
)); |
| 328 |
$content = $view->return_html(); |
| 329 |
$post_data = array( |
| 330 |
'post_title' => wp_strip_all_tags($sample_page_title), |
| 331 |
'post_content' => $content, |
| 332 |
'post_status' => 'draft', |
| 333 |
'post_type' => 'page', |
| 334 |
'post_author' => get_current_user_id(), |
| 335 |
); |
| 336 |
$post_id = wp_insert_post($post_data); |
| 337 |
TagGroups_Options::update_option('tag_group_sample_page_id', $post_id); |
| 338 |
} else { |
| 339 |
delete_option('tag_group_sample_page_id'); |
| 340 |
} |
| 341 |
|
| 342 |
break; |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|