| 1 |
<?php |
| 2 |
/** |
| 3 |
* CartFlows Admin. |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Cartflows_Admin. |
| 10 |
*/ |
| 11 |
class Cartflows_Admin { |
| 12 |
|
| 13 |
/** |
| 14 |
* Calls on initialization |
| 15 |
* |
| 16 |
* @since 1.0.0 |
| 17 |
*/ |
| 18 |
public static function init() { |
| 19 |
|
| 20 |
self::initialise_plugin(); |
| 21 |
self::init_hooks(); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Init Hooks. |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
static public function init_hooks() { |
| 31 |
|
| 32 |
if ( ! is_admin() ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
include_once CARTFLOWS_DIR . 'classes/class-cartflows-admin-fields.php'; |
| 37 |
|
| 38 |
// Add CARTFLOWS menu option to admin. |
| 39 |
add_action( 'network_admin_menu', __CLASS__ . '::menu' ); |
| 40 |
add_action( 'admin_menu', __CLASS__ . '::menu' ); |
| 41 |
add_action( 'admin_menu', __CLASS__ . '::submenu', 999 ); |
| 42 |
|
| 43 |
add_action( 'cartflows_render_admin_content', __CLASS__ . '::render_content' ); |
| 44 |
|
| 45 |
add_action( 'init', __CLASS__ . '::settings_admin_scripts' ); |
| 46 |
|
| 47 |
/* Global Addmin Script */ |
| 48 |
add_action( 'admin_enqueue_scripts', __CLASS__ . '::global_admin_scripts', 20 ); |
| 49 |
|
| 50 |
add_action( 'admin_footer', __CLASS__ . '::global_admin_data', 9555 ); |
| 51 |
|
| 52 |
/* Add lite version class to body */ |
| 53 |
add_action( 'admin_body_class', __CLASS__ . '::add_admin_body_class' ); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* Initialize after Cartflows pro get loaded. |
| 59 |
*/ |
| 60 |
static public function settings_admin_scripts() { |
| 61 |
// Enqueue admin scripts. |
| 62 |
if ( isset( $_REQUEST['page'] ) && CARTFLOWS_SETTINGS == $_REQUEST['page'] ) { |
| 63 |
add_action( 'admin_enqueue_scripts', __CLASS__ . '::styles_scripts' ); |
| 64 |
|
| 65 |
self::save_settings(); |
| 66 |
} |
| 67 |
} |
| 68 |
/** |
| 69 |
* Initialises the Plugin Name. |
| 70 |
* |
| 71 |
* @since 1.0.0 |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
static public function initialise_plugin() { |
| 75 |
|
| 76 |
$name = 'Cartflows'; |
| 77 |
$short_name = 'Cflows'; |
| 78 |
|
| 79 |
define( 'CARTFLOWS_PLUGIN_NAME', $name ); |
| 80 |
define( 'CARTFLOWS_PLUGIN_SHORT_NAME', $short_name ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Renders the admin settings menu. |
| 85 |
* |
| 86 |
* @since 1.0.0 |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
static public function menu() { |
| 90 |
|
| 91 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
add_menu_page( |
| 96 |
'CartFlows', |
| 97 |
'CartFlows', |
| 98 |
'manage_options', |
| 99 |
CARTFLOWS_SLUG, |
| 100 |
__CLASS__ . '::render', |
| 101 |
'data:image/svg+xml;base64,' . base64_encode( file_get_contents( CARTFLOWS_DIR . 'assets/images/cartflows-icon.svg' ) ), |
| 102 |
39.7 |
| 103 |
); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Add submenu to admin menu. |
| 109 |
* |
| 110 |
* @since 1.0.0 |
| 111 |
*/ |
| 112 |
static function submenu() { |
| 113 |
|
| 114 |
$parent_slug = CARTFLOWS_SLUG; |
| 115 |
$page_title = __( 'Settings', 'cartflows' ); |
| 116 |
$menu_title = __( 'Settings', 'cartflows' ); |
| 117 |
$capability = 'manage_options'; |
| 118 |
$menu_slug = 'cartflows_settings'; |
| 119 |
$callback = __CLASS__ . '::render'; |
| 120 |
|
| 121 |
add_submenu_page( |
| 122 |
$parent_slug, |
| 123 |
$page_title, |
| 124 |
$menu_title, |
| 125 |
$capability, |
| 126 |
$menu_slug, |
| 127 |
$callback |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Renders the admin settings. |
| 133 |
* |
| 134 |
* @since 1.0.0 |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
static public function render() { |
| 138 |
$action = ( isset( $_GET['action'] ) ) ? sanitize_text_field( $_GET['action'] ) : ''; |
| 139 |
$action = ( ! empty( $action ) && '' != $action ) ? $action : 'general'; |
| 140 |
$action = str_replace( '_', '-', $action ); |
| 141 |
|
| 142 |
// Enable header icon filter below. |
| 143 |
$header_wrapper_class = apply_filters( 'cartflows_header_wrapper_class', array( $action ) ); |
| 144 |
|
| 145 |
include_once CARTFLOWS_DIR . 'includes/admin/cartflows-admin.php'; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Renders the admin settings content. |
| 150 |
* |
| 151 |
* @since 1.0.0 |
| 152 |
* @return void |
| 153 |
*/ |
| 154 |
static public function render_content() { |
| 155 |
|
| 156 |
$action = ( isset( $_GET['action'] ) ) ? sanitize_text_field( $_GET['action'] ) : ''; |
| 157 |
$action = ( ! empty( $action ) && '' != $action ) ? $action : 'general'; |
| 158 |
$action = str_replace( '_', '-', $action ); |
| 159 |
$action = 'general'; |
| 160 |
|
| 161 |
$header_wrapper_class = apply_filters( 'cartflows_header_wrapper_class', array( $action ) ); |
| 162 |
|
| 163 |
include_once CARTFLOWS_DIR . 'includes/admin/cartflows-general.php'; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Save Global Setting options. |
| 168 |
* |
| 169 |
* @since 1.0.0 |
| 170 |
*/ |
| 171 |
static public function save_common_settings() { |
| 172 |
|
| 173 |
if ( isset( $_POST['cartflows-common-settings-nonce'] ) && wp_verify_nonce( $_POST['cartflows-common-settings-nonce'], 'cartflows-common-settings' ) ) { |
| 174 |
|
| 175 |
$url = $_SERVER['REQUEST_URI']; |
| 176 |
$input_settings = array(); |
| 177 |
$new_settings = array(); |
| 178 |
|
| 179 |
if ( isset( $_POST['_cartflows_common'] ) ) { |
| 180 |
|
| 181 |
$input_settings = $_POST['_cartflows_common']; |
| 182 |
|
| 183 |
// Loop through the input and sanitize each of the values. |
| 184 |
foreach ( $input_settings as $key => $val ) { |
| 185 |
|
| 186 |
if ( is_array( $val ) ) { |
| 187 |
foreach ( $val as $k => $v ) { |
| 188 |
$new_settings[ $key ][ $k ] = ( isset( $val[ $k ] ) ) ? sanitize_text_field( $v ) : ''; |
| 189 |
} |
| 190 |
} else { |
| 191 |
$new_settings[ $key ] = ( isset( $input_settings[ $key ] ) ) ? sanitize_text_field( $val ) : ''; |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
Cartflows_Helper::update_admin_settings_option( '_cartflows_common', $new_settings, true ); |
| 197 |
|
| 198 |
$query = array( |
| 199 |
'message' => 'saved', |
| 200 |
); |
| 201 |
|
| 202 |
$redirect_to = add_query_arg( $query, $url ); |
| 203 |
|
| 204 |
wp_redirect( $redirect_to ); |
| 205 |
exit; |
| 206 |
} // End if statement. |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Check is cartflows admin. |
| 211 |
* |
| 212 |
* @since 1.0.0 |
| 213 |
* @return boolean |
| 214 |
*/ |
| 215 |
static public function is_global_admin() { |
| 216 |
|
| 217 |
$current_screen = get_current_screen(); |
| 218 |
|
| 219 |
if ( |
| 220 |
is_object( $current_screen ) && |
| 221 |
isset( $current_screen->post_type ) && |
| 222 |
( CARTFLOWS_FLOW_POST_TYPE === $current_screen->post_type || |
| 223 |
CARTFLOWS_STEP_POST_TYPE === $current_screen->post_type |
| 224 |
) |
| 225 |
) { |
| 226 |
return true; |
| 227 |
} |
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Check is flow admin. |
| 233 |
* |
| 234 |
* @since 1.0.0 |
| 235 |
* @return boolean |
| 236 |
*/ |
| 237 |
static public function is_flow_edit_admin() { |
| 238 |
|
| 239 |
$current_screen = get_current_screen(); |
| 240 |
|
| 241 |
if ( |
| 242 |
is_object( $current_screen ) && |
| 243 |
isset( $current_screen->post_type ) && |
| 244 |
( CARTFLOWS_FLOW_POST_TYPE === $current_screen->post_type ) && |
| 245 |
isset( $current_screen->base ) && |
| 246 |
( 'post' === $current_screen->base ) |
| 247 |
) { |
| 248 |
return true; |
| 249 |
} |
| 250 |
return false; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Global Admin Scripts. |
| 255 |
* |
| 256 |
* @since 1.0.0 |
| 257 |
*/ |
| 258 |
static public function global_admin_scripts() { |
| 259 |
|
| 260 |
$localize = array( |
| 261 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 262 |
'ajax_nonce' => wp_create_nonce( 'cartflows-nonce' ), |
| 263 |
); |
| 264 |
|
| 265 |
wp_localize_script( 'jquery', 'cartflows_admin', apply_filters( 'cartflows_admin_js_localize', $localize ) ); |
| 266 |
|
| 267 |
if ( self::is_global_admin() ) { |
| 268 |
|
| 269 |
// Styles. |
| 270 |
wp_enqueue_style( 'cartflows-global-admin', CARTFLOWS_URL . 'admin/assets/css/global-admin.css', array(), CARTFLOWS_VER ); |
| 271 |
wp_style_add_data( 'cartflows-global-admin', 'rtl', 'replace' ); |
| 272 |
|
| 273 |
wp_enqueue_script( |
| 274 |
'wcf-global-admin', |
| 275 |
CARTFLOWS_URL . 'admin/assets/js/global-admin.js', |
| 276 |
array( 'jquery' ), |
| 277 |
CARTFLOWS_VER, |
| 278 |
true |
| 279 |
); |
| 280 |
|
| 281 |
do_action( 'cartflows_global_admin_scripts' ); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Global Admin Data. |
| 287 |
* |
| 288 |
* @since 1.0.0 |
| 289 |
*/ |
| 290 |
static public function global_admin_data() { |
| 291 |
|
| 292 |
$current_screen = get_current_screen(); |
| 293 |
|
| 294 |
if ( ! $current_screen ) { |
| 295 |
return; |
| 296 |
} |
| 297 |
|
| 298 |
if ( 'edit-' . CARTFLOWS_FLOW_POST_TYPE != $current_screen->id ) { |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
$default_page_builder = Cartflows_Helper::get_common_setting( 'default_page_builder' ); |
| 303 |
?> |
| 304 |
|
| 305 |
<div id="wcf-remote-flow-importer" class="wcf-templates-popup-overlay"> |
| 306 |
<div class="wcf-templates-popup-content"> |
| 307 |
<div class="spinner"></div> |
| 308 |
<div class="wcf-templates-wrap wcf-templates-wrap-flows"> |
| 309 |
|
| 310 |
<div id="wcf-remote-flow-actions" class="wcf-template-header"> |
| 311 |
<div class="wcf-template-logo-wrap"> |
| 312 |
<span class="wcf-cartflows-logo-img"> |
| 313 |
<span class="cartflows-logo-icon"></span> |
| 314 |
</span> |
| 315 |
<span class="wcf-cartflows-title"><?php _e( 'Flows Library', 'cartflows' ); ?></span> |
| 316 |
</div> |
| 317 |
<div class="wcf-tab-wrapper"> |
| 318 |
<?php if ( 'other' !== $default_page_builder ) { ?> |
| 319 |
<div id="wcf-get-started-steps"> |
| 320 |
<ul class="filter-links "> |
| 321 |
<li> |
| 322 |
<a href="#" class="current" data-slug="ready-templates" data-title="<?php _e( 'Ready Templates', 'cartflows' ); ?>"><?php _e( 'Ready Templates', 'cartflows' ); ?></a> |
| 323 |
</li> |
| 324 |
<li> |
| 325 |
<a href="#" data-slug="canvas" data-title="<?php _e( 'Create Your Own', 'cartflows' ); ?>"><?php _e( 'Create Your Own', 'cartflows' ); ?></a> |
| 326 |
</li> |
| 327 |
</ul> |
| 328 |
</div> |
| 329 |
<?php } ?> |
| 330 |
</div> |
| 331 |
<div class="wcf-popup-close-wrap"> |
| 332 |
<span class="close-icon"><span class="wcf-cartflow-icons dashicons dashicons-no"></span></span> |
| 333 |
</div> |
| 334 |
</div> |
| 335 |
<!-- <div class="wcf-search-form"> |
| 336 |
<label class="screen-reader-text" for="wp-filter-search-input"><?php _e( 'Search Sites', 'cartflows' ); ?> </label> |
| 337 |
<input placeholder="<?php _e( 'Search Flow...', 'cartflows' ); ?>" type="text" aria-describedby="live-search-desc" class="wcf-flow-search-input"> |
| 338 |
</div> --> |
| 339 |
|
| 340 |
<div id="wcf-remote-content"> |
| 341 |
<?php if ( 'other' !== $default_page_builder ) { ?> |
| 342 |
<div id="wcf-ready-templates"> |
| 343 |
<div id="wcf-remote-filters"> |
| 344 |
<div id="wcf-page-builders"></div> |
| 345 |
<div id="wcf-categories"></div> |
| 346 |
</div> |
| 347 |
<div class="wcf-page-builder-notice"></div> |
| 348 |
<div id="wcf-remote-flow-list" class="wcf-remote-list wcf-template-list-wrap"><span class="spinner is-active"></span></div> |
| 349 |
<div id="wcf-upcoming-page-builders" style="display: none;" class="wcf-remote-list wcf-template-list-wrap"></div> |
| 350 |
</div> |
| 351 |
<?php } ?> |
| 352 |
<div id="wcf-start-from-scratch" style="<?php echo ( 'other' !== $default_page_builder ) ? 'display: none;' : ''; ?>"> |
| 353 |
<div class="inner"> |
| 354 |
<a href="#" class="button button-hero button-primary cartflows-flow-import-blank"><?php _e( 'Design Your Flow', 'cartflows' ); ?></a> |
| 355 |
<p class="wcf-learn-how"><a href="https://cartflows.com/docs/cartflows-step-types/" target="_blank"><?php _e( 'Learn How', 'cartflows' ); ?> <i class="dashicons dashicons-external"></i></a></p> |
| 356 |
</div> |
| 357 |
</div> |
| 358 |
</div> |
| 359 |
</div> |
| 360 |
</div> |
| 361 |
</div> |
| 362 |
|
| 363 |
<?php |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Enqueues the needed CSS/JS for the builder's admin settings page. |
| 368 |
* |
| 369 |
* @since 1.0.0 |
| 370 |
*/ |
| 371 |
static public function styles_scripts() { |
| 372 |
|
| 373 |
// Styles. |
| 374 |
wp_enqueue_style( 'cartflows-admin-settings', CARTFLOWS_URL . 'admin/assets/css/admin-menu-settings.css', array(), CARTFLOWS_VER ); |
| 375 |
wp_style_add_data( 'cartflows-admin-settings', 'rtl', 'replace' ); |
| 376 |
|
| 377 |
// Script. |
| 378 |
wp_enqueue_script( 'cartflows-admin-settings', CARTFLOWS_URL . 'admin/assets/js/admin-menu-settings.js', array( 'jquery', 'wp-util', 'updates' ), CARTFLOWS_VER ); |
| 379 |
|
| 380 |
$localize = array( |
| 381 |
'ajax_nonce' => wp_create_nonce( 'cartflows-widget-nonce' ), |
| 382 |
); |
| 383 |
|
| 384 |
wp_localize_script( 'cartflows-admin-settings', 'cartflows', apply_filters( 'cartflows_js_localize', $localize ) ); |
| 385 |
|
| 386 |
do_action( 'cartflows_admin_settings_after_enqueue_scripts' ); |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* Save All admin settings here |
| 391 |
*/ |
| 392 |
static public function save_settings() { |
| 393 |
|
| 394 |
// Only admins can save settings. |
| 395 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 396 |
return; |
| 397 |
} |
| 398 |
|
| 399 |
self::save_common_settings(); |
| 400 |
|
| 401 |
// Let extensions hook into saving. |
| 402 |
do_action( 'cartflows_admin_settings_save' ); |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* Get and return page URL |
| 407 |
* |
| 408 |
* @param string $menu_slug Menu name. |
| 409 |
* @since 1.0.0 |
| 410 |
* @return string page url |
| 411 |
*/ |
| 412 |
static public function get_page_url( $menu_slug ) { |
| 413 |
|
| 414 |
$parent_page = self::$default_menu_position; |
| 415 |
|
| 416 |
if ( strpos( $parent_page, '?' ) !== false ) { |
| 417 |
$query_var = '&page=' . self::$plugin_slug; |
| 418 |
} else { |
| 419 |
$query_var = '?page=' . self::$plugin_slug; |
| 420 |
} |
| 421 |
|
| 422 |
$parent_page_url = admin_url( $parent_page . $query_var ); |
| 423 |
|
| 424 |
$url = $parent_page_url . '&action=' . $menu_slug; |
| 425 |
|
| 426 |
return esc_url( $url ); |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* Admin body classes. |
| 431 |
* |
| 432 |
* Body classes to be added to <body> tag in admin page |
| 433 |
* |
| 434 |
* @param String $classes body classes returned from the filter. |
| 435 |
* @return String body classes to be added to <body> tag in admin page |
| 436 |
*/ |
| 437 |
static public function add_admin_body_class( $classes ) { |
| 438 |
|
| 439 |
$classes .= ' cartflows-' . CARTFLOWS_VER; |
| 440 |
|
| 441 |
return $classes; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
Cartflows_Admin::init(); |
| 446 |
|