| 1 |
<?php |
| 2 |
/** |
| 3 |
* CartFlows Admin. |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
/** |
| 13 |
* Class Cartflows_Admin. |
| 14 |
*/ |
| 15 |
class Cartflows_Admin { |
| 16 |
|
| 17 |
/** |
| 18 |
* Instance |
| 19 |
* |
| 20 |
* @access private |
| 21 |
* @var object Class object. |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
private static $instance; |
| 25 |
|
| 26 |
/** |
| 27 |
* Initiator |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
* @return object initialized object of class. |
| 31 |
*/ |
| 32 |
public static function get_instance() { |
| 33 |
if ( ! isset( self::$instance ) ) { |
| 34 |
self::$instance = new self(); |
| 35 |
} |
| 36 |
return self::$instance; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Constructor |
| 41 |
*/ |
| 42 |
public function __construct() { |
| 43 |
|
| 44 |
$this->init_hooks(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Init Hooks. |
| 49 |
* |
| 50 |
* @since 1.0.0 |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public function init_hooks() { |
| 54 |
|
| 55 |
if ( ! is_admin() ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
/* Add lite version class to body */ |
| 60 |
add_action( 'admin_body_class', array( $this, 'add_admin_body_class' ) ); |
| 61 |
|
| 62 |
add_filter( 'plugin_action_links_' . CARTFLOWS_BASE, array( $this, 'add_action_links' ) ); |
| 63 |
|
| 64 |
add_action( 'admin_init', array( $this, 'flush_rules_after_save_permalinks' ) ); |
| 65 |
|
| 66 |
add_filter( 'post_row_actions', array( $this, 'remove_flow_actions' ), 99, 2 ); |
| 67 |
|
| 68 |
add_action( 'wp_dashboard_setup', array( $this, 'dashboard_widget' ) ); |
| 69 |
|
| 70 |
add_action( 'init', array( $this, 'run_scheduled_docs_job' ) ); |
| 71 |
add_action( 'cartflows_update_knowledge_base_data', array( $this, 'cartflows_update_knowledge_base_data' ) ); |
| 72 |
add_action( 'admin_post_cartflows_rollback', array( $this, 'post_cartflows_rollback' ) ); |
| 73 |
|
| 74 |
$this->do_not_cache_admin_pages_actions(); |
| 75 |
|
| 76 |
// Provide pointer configuration via filter. |
| 77 |
add_filter( 'cartflows_pointer_config', array( $this, 'get_pointer_config' ) ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Do not cache admin pages actions. |
| 82 |
* |
| 83 |
* @since 1.6.13 |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
public function do_not_cache_admin_pages_actions() { |
| 87 |
|
| 88 |
add_action( 'admin_init', array( $this, 'add_admin_cache_restrict_const' ) ); |
| 89 |
add_action( 'admin_head', array( $this, 'add_admin_cache_restrict_meta' ) ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Run scheduled job for CartFLows knowledge base data. |
| 94 |
* |
| 95 |
* @since 2.0.0 |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function run_scheduled_docs_job() { |
| 99 |
if ( false === as_next_scheduled_action( 'cartflows_update_knowledge_base_data' ) && ! wp_installing() ) { |
| 100 |
as_schedule_recurring_action( time(), WEEK_IN_SECONDS, 'cartflows_update_knowledge_base_data' ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Cartflows's REST knowledge base data. |
| 106 |
* |
| 107 |
* @since 2.0.0 |
| 108 |
* @return mixed |
| 109 |
*/ |
| 110 |
public static function cartflows_update_knowledge_base_data() { |
| 111 |
$docs_json = json_decode( wp_remote_retrieve_body( wp_remote_get( 'https://cartflows.com//wp-json/powerful-docs/v1/get-docs' ) ) ); |
| 112 |
Cartflows_Helper::update_admin_settings_option( 'cartflows_docs_data', $docs_json ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Init dashboard widgets. |
| 117 |
*/ |
| 118 |
public function dashboard_widget() { |
| 119 |
|
| 120 |
if ( current_user_can( 'manage_options' ) && '1' === get_option( 'wcf_setup_skipped', false ) && '1' !== get_option( 'wcf_setup_complete', false ) ) { |
| 121 |
wp_add_dashboard_widget( |
| 122 |
'cartFlows_setup_dashboard_widget', |
| 123 |
__( 'CartFlows Setup', 'cartflows' ), |
| 124 |
array( $this, 'status_widget' ) |
| 125 |
); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Show status widget. |
| 131 |
*/ |
| 132 |
public function status_widget() { |
| 133 |
|
| 134 |
$admin_url = admin_url( 'index.php' ) . '?page=cartflows-onboarding&'; |
| 135 |
|
| 136 |
$steps = array( |
| 137 |
'welcome' => $admin_url . 'step=welcome', |
| 138 |
'page-builder' => $admin_url . 'step=page-builder', |
| 139 |
'plugin-install' => $admin_url . 'step=plugin-install', |
| 140 |
'global-checkout' => $admin_url . 'step=global-checkout', |
| 141 |
'optin' => $admin_url . 'step=optin', |
| 142 |
'ready' => $admin_url . 'step=ready', |
| 143 |
); |
| 144 |
|
| 145 |
$exit_step = get_option( 'wcf_exit_setup_step', 'welcome' ); |
| 146 |
|
| 147 |
$incompleted_steps = array_search( $exit_step, array_keys( $steps ), true ); |
| 148 |
$remianing_steps = array_slice( $steps, 0, $incompleted_steps ); |
| 149 |
|
| 150 |
$completed_tasks_count = count( $remianing_steps ) + 1; |
| 151 |
$tasks_count = count( $steps ); |
| 152 |
$button_link = ! empty( $exit_step ) ? $steps[ $exit_step ] : ''; |
| 153 |
|
| 154 |
$progress_percentage = ( $completed_tasks_count / $tasks_count ) * 100; |
| 155 |
$circle_r = 6.5; |
| 156 |
$circle_dashoffset = ( ( 100 - $progress_percentage ) / 100 ) * ( pi() * ( $circle_r * 2 ) ); |
| 157 |
|
| 158 |
wp_enqueue_style( 'cartflows-dashboard-widget', CARTFLOWS_URL . 'admin/assets/css/admin-widget.css', array(), CARTFLOWS_VER ); |
| 159 |
|
| 160 |
?> |
| 161 |
|
| 162 |
<div class="cartflows-dashboard-widget-finish-setup"> |
| 163 |
<span class='progress-wrapper'> |
| 164 |
<svg class="circle-progress" width="17" height="17" version="1.1" xmlns="http://www.w3.org/2000/svg"> |
| 165 |
<circle r="6.5" cx="10" cy="10" fill="transparent" stroke-dasharray="40.859" stroke-dashoffset="0"></circle> |
| 166 |
<circle class="bar" r="6.5" cx="190" cy="10" fill="transparent" stroke-dasharray="40.859" stroke-dashoffset="<?php echo esc_attr( $circle_dashoffset ); ?>" transform='rotate(-90 100 100)'></circle> |
| 167 |
</svg> |
| 168 |
<span><?php echo esc_html_e( 'Step', 'cartflows' ); ?> <?php echo esc_html( $completed_tasks_count ); ?> <?php echo esc_html_e( 'of', 'cartflows' ); ?> <?php echo esc_html( $tasks_count ); ?></span> |
| 169 |
</span> |
| 170 |
|
| 171 |
<div class="description"> |
| 172 |
<div class="wcf-left-column"> |
| 173 |
<p> |
| 174 |
<?php echo esc_html_e( 'You\'re almost there! Once you complete CartFlows setup you can start receiving orders from flows.', 'cartflows' ); ?> |
| 175 |
</p> |
| 176 |
<a href='<?php echo esc_url( $button_link ); ?>' class='button button-primary'><?php echo esc_html_e( 'Complete Setup', 'cartflows' ); ?></a> |
| 177 |
</div> |
| 178 |
<div class="wcf-right-column"> |
| 179 |
<img src="<?php echo esc_url( CARTFLOWS_URL . 'admin/assets/images/cartflows-home-widget.svg' ); ?>" /> |
| 180 |
</div> |
| 181 |
</div> |
| 182 |
</div> |
| 183 |
<?php |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Add the clone link to action list for flows row actions |
| 188 |
* |
| 189 |
* @param array $actions Actions array. |
| 190 |
* @param object $post Post object. |
| 191 |
* |
| 192 |
* @return array |
| 193 |
*/ |
| 194 |
public function remove_flow_actions( $actions, $post ) { |
| 195 |
|
| 196 |
if ( current_user_can( 'edit_posts' ) && isset( $post ) && CARTFLOWS_FLOW_POST_TYPE === $post->post_type ) { |
| 197 |
|
| 198 |
if ( isset( $actions['duplicate'] ) ) { // Duplicate page plugin remove. |
| 199 |
unset( $actions['duplicate'] ); |
| 200 |
} |
| 201 |
|
| 202 |
if ( isset( $actions['edit_as_new_draft'] ) ) { // Duplicate post plugin remove. |
| 203 |
unset( $actions['edit_as_new_draft'] ); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
return $actions; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* After save of permalinks. |
| 212 |
*/ |
| 213 |
public static function flush_rules_after_save_permalinks() { |
| 214 |
|
| 215 |
$has_saved_permalinks = get_option( 'cartflows_permalink_refresh' ); |
| 216 |
if ( $has_saved_permalinks ) { |
| 217 |
// Required to flush rules when permalink changes. |
| 218 |
flush_rewrite_rules(); //phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.flush_rewrite_rules_flush_rewrite_rules |
| 219 |
delete_option( 'cartflows_permalink_refresh' ); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Show action on plugin page. |
| 225 |
* |
| 226 |
* @param array $links links. |
| 227 |
* @return array |
| 228 |
*/ |
| 229 |
public function add_action_links( $links ) { |
| 230 |
|
| 231 |
$mylinks = array( |
| 232 |
'<a target="_blank" href="' . esc_url( |
| 233 |
Cartflows_Helper::get_kb_doc_link( |
| 234 |
'https://cartflows.com/docs/', |
| 235 |
array( |
| 236 |
'utm_source' => 'plugin-page', |
| 237 |
'utm_campaign' => 'go-pro', |
| 238 |
) |
| 239 |
) |
| 240 |
) . '">' . __( 'Docs', 'cartflows' ) . '</a>', |
| 241 |
); |
| 242 |
|
| 243 |
if ( current_user_can( 'cartflows_manage_settings' ) ) { |
| 244 |
|
| 245 |
$is_legacy_admin = defined( 'CARTFLOWS_LEGACY_ADMIN' ) && CARTFLOWS_LEGACY_ADMIN; |
| 246 |
|
| 247 |
$settings_args = array( 'page' => CARTFLOWS_SLUG ); |
| 248 |
|
| 249 |
if ( $is_legacy_admin ) { |
| 250 |
$settings_args['settings'] = true; |
| 251 |
} else { |
| 252 |
$settings_args['path'] = 'settings'; |
| 253 |
} |
| 254 |
|
| 255 |
$default_url = add_query_arg( $settings_args, admin_url( 'admin.php' ) ); |
| 256 |
|
| 257 |
array_unshift( $mylinks, '<a href="' . esc_url( $default_url ) . '">' . __( 'Settings', 'cartflows' ) . '</a>' ); |
| 258 |
} |
| 259 |
|
| 260 |
if ( ! _is_cartflows_pro() ) { |
| 261 |
array_push( |
| 262 |
$mylinks, |
| 263 |
'<a style="color: #39b54a; font-weight: 700;" target="_blank" href="' . esc_url( |
| 264 |
Cartflows_Helper::get_upgrade_to_pro_link( 'pricing', '', array( 'utm_source' => 'plugin-page' ) ) |
| 265 |
) . '"> ' . __( 'Get CartFlows Pro', 'cartflows' ) . ' </a>' |
| 266 |
); |
| 267 |
} |
| 268 |
|
| 269 |
return array_merge( $links, $mylinks ); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Check is flow admin. |
| 274 |
* |
| 275 |
* @since 1.0.0 |
| 276 |
* @return boolean |
| 277 |
*/ |
| 278 |
public static function is_flow_edit_admin() { |
| 279 |
|
| 280 |
$current_screen = get_current_screen(); |
| 281 |
|
| 282 |
if ( |
| 283 |
is_object( $current_screen ) && |
| 284 |
isset( $current_screen->post_type ) && |
| 285 |
( CARTFLOWS_FLOW_POST_TYPE === $current_screen->post_type ) && |
| 286 |
isset( $current_screen->base ) && |
| 287 |
( 'post' === $current_screen->base ) |
| 288 |
) { |
| 289 |
return true; |
| 290 |
} |
| 291 |
return false; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Admin body classes. |
| 296 |
* |
| 297 |
* Body classes to be added to <body> tag in admin page |
| 298 |
* |
| 299 |
* @param String $classes body classes returned from the filter. |
| 300 |
* @return String body classes to be added to <body> tag in admin page |
| 301 |
*/ |
| 302 |
public function add_admin_body_class( $classes ) { |
| 303 |
|
| 304 |
$classes .= ' cartflows-' . CARTFLOWS_VER; |
| 305 |
|
| 306 |
if ( isset( $_GET['action'] ) && in_array( sanitize_text_field( wp_unslash( $_GET['action'] ) ), array( 'wcf-log', 'wcf-license' ) ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 307 |
$classes .= ' wcf-debug-page '; |
| 308 |
} |
| 309 |
|
| 310 |
return $classes; |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Check allowed screen for notices. |
| 315 |
* |
| 316 |
* @since 1.0.0 |
| 317 |
* @return bool |
| 318 |
*/ |
| 319 |
public function allowed_screen_for_notices() { |
| 320 |
|
| 321 |
$screen = get_current_screen(); |
| 322 |
$screen_id = $screen ? $screen->id : ''; |
| 323 |
$allowed_screens = array( |
| 324 |
'toplevel_page_cartflows', |
| 325 |
'edit-cartflows_flow', |
| 326 |
); |
| 327 |
|
| 328 |
if ( in_array( $screen_id, $allowed_screens, true ) ) { |
| 329 |
return true; |
| 330 |
} |
| 331 |
|
| 332 |
return false; |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Function to add the meta and headers for not to cache the CartFlows wp-admin page. |
| 337 |
* |
| 338 |
* @return void |
| 339 |
*/ |
| 340 |
public function add_admin_cache_restrict_meta() { |
| 341 |
|
| 342 |
if ( ! $this->allowed_screen_for_notices() ) { |
| 343 |
return; |
| 344 |
} |
| 345 |
|
| 346 |
echo '<!-- Added by CartFlows -->'; |
| 347 |
echo '<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" /> <meta http-equiv="Pragma" content="no-cache" /> <meta http-equiv="Expires" content="0" />'; |
| 348 |
echo '<!-- Added by CartFlows -->'; |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Function to add the Do Not Cache Constants on the CartFlows admin pages. |
| 353 |
* |
| 354 |
* @return void |
| 355 |
*/ |
| 356 |
public function add_admin_cache_restrict_const() { |
| 357 |
// Ignoring nonce verification as using SuperGlobal variables on WordPress hooks. |
| 358 |
if ( isset( $_GET['page'] ) && ( 'cartflows' === sanitize_text_field( $_GET['page'] ) || false !== strpos( sanitize_text_field( $_GET['page'] ), 'cartflows_' ) ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 359 |
wcf()->utils->get_cache_headers(); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* CartFlows version rollback. |
| 365 |
* |
| 366 |
* Rollback to previous CartFlows version. |
| 367 |
* |
| 368 |
* Fired by `admin_post_cartflows_rollback` action. |
| 369 |
* |
| 370 |
* @since 2.1.6 |
| 371 |
* @access public |
| 372 |
* @return void |
| 373 |
*/ |
| 374 |
public function post_cartflows_rollback() { |
| 375 |
|
| 376 |
if ( ! current_user_can( 'install_plugins' ) ) { |
| 377 |
wp_die( |
| 378 |
esc_html__( 'You do not have permission to access this page.', 'cartflows' ), |
| 379 |
esc_html__( 'Rollback to Previous Version', 'cartflows' ), |
| 380 |
array( |
| 381 |
'response' => 200, |
| 382 |
) |
| 383 |
); |
| 384 |
} |
| 385 |
|
| 386 |
check_admin_referer( 'cartflows_rollback' ); |
| 387 |
|
| 388 |
$rollback_versions = \Cartflows_Helper::get_rollback_versions(); |
| 389 |
$update_version = isset( $_GET['version'] ) ? sanitize_text_field( $_GET['version'] ) : ''; |
| 390 |
|
| 391 |
if ( empty( $update_version ) || ! in_array( $update_version, $rollback_versions, true ) ) { |
| 392 |
wp_die( esc_html__( 'Error occurred, The version selected is invalid. Try selecting different version.', 'cartflows' ) ); |
| 393 |
} |
| 394 |
|
| 395 |
$plugin_slug = basename( CARTFLOWS_FILE, '.php' ); |
| 396 |
|
| 397 |
$rollback = new Cartflows_Rollback( |
| 398 |
array( |
| 399 |
'version' => $update_version, |
| 400 |
'plugin_name' => CARTFLOWS_BASE, |
| 401 |
'plugin_slug' => $plugin_slug, |
| 402 |
'package_url' => sprintf( 'https://downloads.wordpress.org/plugin/%s.%s.zip', $plugin_slug, $update_version ), |
| 403 |
) |
| 404 |
); |
| 405 |
|
| 406 |
$rollback->run(); |
| 407 |
|
| 408 |
wp_die( |
| 409 |
'', |
| 410 |
esc_html__( 'Rollback to Previous Version', 'cartflows' ), |
| 411 |
array( |
| 412 |
'response' => 200, |
| 413 |
) |
| 414 |
); |
| 415 |
} |
| 416 |
|
| 417 |
/** |
| 418 |
* Provide CartFlows Pointer configuration via filter. |
| 419 |
* |
| 420 |
* @param array<string, mixed> $config Existing configuration. |
| 421 |
* @return array<string, mixed> Pointer configuration. |
| 422 |
*/ |
| 423 |
public function get_pointer_config( $config ) { |
| 424 |
return array( |
| 425 |
'option_name' => 'cartflows_pointer_data', |
| 426 |
'title' => __( 'CartFlows is waiting for you!', 'cartflows' ), |
| 427 |
'content' => sprintf( |
| 428 |
/* translators: %1$s opening <strong> tag, %2$s closing </strong> tag */ |
| 429 |
__( '%1$sLaunch your first funnel in minutes.%2$s Follow a guided setup to start capturing leads or sales immediately.', 'cartflows' ), |
| 430 |
'<strong>', |
| 431 |
'</strong>' |
| 432 |
), |
| 433 |
'button_text' => __( 'Create My First Funnel', 'cartflows' ), |
| 434 |
'button_url' => admin_url( 'admin.php?page=cartflows' ), |
| 435 |
'dismiss_text' => __( 'Dismiss', 'cartflows' ), |
| 436 |
'target_selector' => '#toplevel_page_cartflows', |
| 437 |
'fallback_target' => '#menu-plugins', |
| 438 |
'allowed_pages' => array( 'index.php', 'plugins.php' ), |
| 439 |
'post_type' => CARTFLOWS_FLOW_POST_TYPE, |
| 440 |
'max_posts' => 0, |
| 441 |
); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
Cartflows_Admin::get_instance(); |
| 446 |
|