| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class that sets up the WordPress Campaign Builder Wizard. |
| 4 |
* |
| 5 |
* @package Charitable |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.8.0 |
| 10 |
* @version 1.8.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Campaign_Congrats_Wizard' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Embed Form in a Page wizard. |
| 22 |
* |
| 23 |
* @since 1.8.0 |
| 24 |
*/ |
| 25 |
class Charitable_Campaign_Congrats_Wizard { |
| 26 |
|
| 27 |
/** |
| 28 |
* One is the loneliest number that you'll ever do. |
| 29 |
* |
| 30 |
* @since 1.8.0 |
| 31 |
* |
| 32 |
* @var object |
| 33 |
*/ |
| 34 |
private static $instance; |
| 35 |
|
| 36 |
/** |
| 37 |
* Max search results count of 'Select Page' dropdown. |
| 38 |
* |
| 39 |
* @since 1.8.0 |
| 40 |
* |
| 41 |
* @var int |
| 42 |
*/ |
| 43 |
const MAX_SEARCH_RESULTS_DROPDOWN_PAGES_COUNT = 20; |
| 44 |
|
| 45 |
/** |
| 46 |
* Post statuses of pages in 'Select Page' dropdown. |
| 47 |
* |
| 48 |
* @since 1.8.0 |
| 49 |
* |
| 50 |
* @var string[] |
| 51 |
*/ |
| 52 |
const POST_STATUSES_OF_DROPDOWN_PAGES = [ 'publish', 'pending' ]; |
| 53 |
|
| 54 |
/** |
| 55 |
* Main Instance. |
| 56 |
* |
| 57 |
* @since 1.8.0 |
| 58 |
* |
| 59 |
* @return Charitable_Builder |
| 60 |
*/ |
| 61 |
public static function get_instance() { |
| 62 |
|
| 63 |
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Charitable_Campaign_Congrats_Wizard ) ) { |
| 64 |
|
| 65 |
self::$instance = new Charitable_Campaign_Congrats_Wizard(); |
| 66 |
|
| 67 |
add_action( 'admin_init', [ self::$instance, 'init' ], 10 ); |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
return self::$instance; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Create object instance. |
| 76 |
* |
| 77 |
* @since 1.8.0 |
| 78 |
*/ |
| 79 |
private function __construct() { |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Initialize class. |
| 84 |
* |
| 85 |
* @since 1.8.0 |
| 86 |
*/ |
| 87 |
public function init() { |
| 88 |
|
| 89 |
if ( ! isset( $_GET['page'] ) || $_GET['page'] !== 'charitable-campaign-builder' ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
// Campaign Embed Wizard should load only in the Campaign Builder and on the Edit/Add Page screen. |
| 94 |
if ( |
| 95 |
! $this->campaign_is_campaign_builder_admin_page( 'builder' ) && |
| 96 |
! wp_doing_ajax() && |
| 97 |
! $this->is_campaign_congrats_page() |
| 98 |
) { |
| 99 |
|
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
$this->hooks(); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Determine if we are on the campaign builder admin page |
| 108 |
* |
| 109 |
* @since 1.8.0 |
| 110 |
*/ |
| 111 |
public function campaign_is_campaign_builder_admin_page() { |
| 112 |
|
| 113 |
if ( isset( $_POST['campaign_id'] ) ) { // phpcs:ignore |
| 114 |
return true; |
| 115 |
} |
| 116 |
|
| 117 |
if ( ! isset( $_GET['page'] ) || $_GET['page'] !== 'charitable-campaign-builder' ) { // phpcs:ignore |
| 118 |
return false; |
| 119 |
} else { |
| 120 |
return true; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Register hooks. |
| 126 |
* |
| 127 |
* @since 1.8.0 |
| 128 |
*/ |
| 129 |
public function hooks() { |
| 130 |
|
| 131 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueues' ] ); |
| 132 |
add_action( 'admin_footer', [ $this, 'output' ] ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Enqueue assets. |
| 137 |
* |
| 138 |
* @since 1.8.0 |
| 139 |
*/ |
| 140 |
public function enqueues() { |
| 141 |
|
| 142 |
$min = charitable_get_min_suffix(); |
| 143 |
$style_version = charitable_get_style_version(); |
| 144 |
|
| 145 |
wp_enqueue_style( |
| 146 |
'campaign-admin-campaign-congrats-wizard', |
| 147 |
charitable()->get_path( 'directory', false ) . "assets/css/campaign-builder/campaign-congrats-wizard{$min}.css", |
| 148 |
null, |
| 149 |
$style_version |
| 150 |
); |
| 151 |
|
| 152 |
wp_enqueue_script( |
| 153 |
'campaign-admin-congrats-wizard', |
| 154 |
charitable()->get_path( 'directory', false ) . "assets/js/campaign-builder/campaign-congrats-wizard{$min}.js", |
| 155 |
[ 'jquery', 'underscore' ], |
| 156 |
$style_version |
| 157 |
); |
| 158 |
|
| 159 |
wp_enqueue_script( |
| 160 |
'campaign-admin-congrats-wizard-confetti', |
| 161 |
charitable()->get_path( 'directory', false ) . 'assets/js/libraries/confetti.min.js', |
| 162 |
[ 'jquery', 'campaign-admin-congrats-wizard' ], |
| 163 |
'1.8.0' |
| 164 |
); |
| 165 |
|
| 166 |
wp_localize_script( |
| 167 |
'campaign-admin-campaign-congrats-wizard', |
| 168 |
'campaign_admin_campaign_congrats_wizard', |
| 169 |
[ |
| 170 |
'nonce' => wp_create_nonce( 'campaign_admin_campaign_congrats_wizard_nonce' ), |
| 171 |
'is_edit_page' => (int) $this->is_campaign_congrats_page( 'edit' ), |
| 172 |
] |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Output HTML. |
| 178 |
* |
| 179 |
* @since 1.8.0 |
| 180 |
*/ |
| 181 |
public function output() { |
| 182 |
|
| 183 |
$template = charitable()->get_path( 'directory', true ) . 'includes/admin/campaign-builder/campaign-congrats-wizard/popup.php'; |
| 184 |
$view_args = []; |
| 185 |
|
| 186 |
$args['user_can_edit_pages'] = current_user_can( 'edit_pages' ); |
| 187 |
$args['dropdown_pages'] = $this->get_select_dropdown_pages_html(); |
| 188 |
|
| 189 |
include $template; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Check if the current page is a campaign embed page. |
| 194 |
* |
| 195 |
* @since 1.8.0 |
| 196 |
* |
| 197 |
* @param string $type Type of the embed page to check. Can be '', 'add' or 'edit'. By default is empty string. |
| 198 |
* |
| 199 |
* @return boolean |
| 200 |
*/ |
| 201 |
public function is_campaign_congrats_page( $type = '' ) { |
| 202 |
|
| 203 |
global $pagenow; |
| 204 |
|
| 205 |
$type = $type === 'add' || $type === 'edit' ? $type : ''; |
| 206 |
|
| 207 |
if ( |
| 208 |
$pagenow !== 'post.php' && |
| 209 |
$pagenow !== 'post-new.php' |
| 210 |
) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
// phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 215 |
$post_id = empty( $_GET['post'] ) ? 0 : (int) $_GET['post']; |
| 216 |
$post_type = empty( $_GET['post_type'] ) ? '' : sanitize_key( $_GET['post_type'] ); |
| 217 |
$action = empty( $_GET['action'] ) ? 'add' : sanitize_key( $_GET['action'] ); |
| 218 |
// phpcs:enable |
| 219 |
|
| 220 |
if ( $pagenow === 'post-new.php' && |
| 221 |
( empty( $post_type ) || $post_type !== 'page' ) |
| 222 |
) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
if ( |
| 227 |
$pagenow === 'post.php' && |
| 228 |
( empty( $post_id ) || get_post_type( $post_id ) !== 'page' ) |
| 229 |
) { |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
$meta = $this->get_meta(); |
| 234 |
$embed_page = ! empty( $meta['embed_page'] ) ? (int) $meta['embed_page'] : 0; |
| 235 |
|
| 236 |
if ( 'add' === $action && 0 === $embed_page && $type !== 'edit' ) { |
| 237 |
return true; |
| 238 |
} |
| 239 |
|
| 240 |
if ( ! empty( $post_id ) && $embed_page === $post_id && $type !== 'add' ) { |
| 241 |
return true; |
| 242 |
} |
| 243 |
|
| 244 |
return false; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Set user's embed meta data. |
| 249 |
* |
| 250 |
* @since 1.8.0 |
| 251 |
* |
| 252 |
* @param array $data Data array to set. |
| 253 |
*/ |
| 254 |
public function set_meta( $data ) { |
| 255 |
|
| 256 |
update_user_meta( get_current_user_id(), 'campaign_admin_campaign_congrats_wizard', $data ); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Get user's embed meta data. |
| 261 |
* |
| 262 |
* @since 1.8.0 |
| 263 |
* |
| 264 |
* @return array User's embed meta data. |
| 265 |
*/ |
| 266 |
public function get_meta() { |
| 267 |
|
| 268 |
return get_user_meta( get_current_user_id(), 'campaign_admin_campaign_congrats_wizard', true ); |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Delete user's embed meta data. |
| 273 |
* |
| 274 |
* @since 1.8.0 |
| 275 |
*/ |
| 276 |
public function delete_meta() { |
| 277 |
|
| 278 |
delete_user_meta( get_current_user_id(), 'campaign_admin_campaign_congrats_wizard' ); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Get embed page URL via AJAX. |
| 283 |
* |
| 284 |
* @since 1.8.0 |
| 285 |
*/ |
| 286 |
public function get_congrats_page_url_ajax() { |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Generate select with pages which are available to edit for current user. |
| 291 |
* |
| 292 |
* @since 1.8.0 |
| 293 |
* |
| 294 |
* @return string |
| 295 |
*/ |
| 296 |
private function get_select_dropdown_pages_html() { |
| 297 |
|
| 298 |
$dropdown_pages = charitable_campaign_search_posts( |
| 299 |
'', |
| 300 |
[ |
| 301 |
'count' => self::MAX_SEARCH_RESULTS_DROPDOWN_PAGES_COUNT, |
| 302 |
'post_status' => self::POST_STATUSES_OF_DROPDOWN_PAGES, |
| 303 |
] |
| 304 |
); |
| 305 |
|
| 306 |
if ( empty( $dropdown_pages ) ) { |
| 307 |
return ''; |
| 308 |
} |
| 309 |
|
| 310 |
$total_pages = 0; |
| 311 |
$wp_count_pages = (array) wp_count_posts( 'page' ); |
| 312 |
|
| 313 |
foreach ( $wp_count_pages as $page_status => $pages_count ) { |
| 314 |
if ( in_array( $page_status, self::POST_STATUSES_OF_DROPDOWN_PAGES, true ) ) { |
| 315 |
$total_pages += $pages_count; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
$return_this = charitable_settings_select_callback( |
| 320 |
[ |
| 321 |
'id' => 'campaign-congrats-wizard-choicesjs-select-pages', |
| 322 |
'type' => 'select', |
| 323 |
'choicesjs' => true, |
| 324 |
'options' => wp_list_pluck( $dropdown_pages, 'post_title', 'ID' ), |
| 325 |
'data' => [ |
| 326 |
'use_ajax' => $total_pages > self::MAX_SEARCH_RESULTS_DROPDOWN_PAGES_COUNT, |
| 327 |
], |
| 328 |
] |
| 329 |
); |
| 330 |
|
| 331 |
return $return_this; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
endif; |
| 337 |
|