| 1 |
<?php |
| 2 |
/** |
| 3 |
* Flow post type |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
/** |
| 13 |
* Initialization |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
class Cartflows_Flow_Post_Type { |
| 18 |
|
| 19 |
|
| 20 |
/** |
| 21 |
* Member Variable |
| 22 |
* |
| 23 |
* @var instance |
| 24 |
*/ |
| 25 |
private static $instance; |
| 26 |
|
| 27 |
/** |
| 28 |
* Initiator |
| 29 |
*/ |
| 30 |
public static function get_instance() { |
| 31 |
if ( ! isset( self::$instance ) ) { |
| 32 |
self::$instance = new self(); |
| 33 |
} |
| 34 |
return self::$instance; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
|
| 42 |
add_action( 'init', array( $this, 'flow_post_type' ) ); |
| 43 |
|
| 44 |
add_action( 'do_meta_boxes', array( $this, 'wcf_change_metabox_position' ) ); |
| 45 |
|
| 46 |
add_filter( 'post_updated_messages', array( $this, 'custom_post_type_post_update_messages' ) ); |
| 47 |
|
| 48 |
add_filter( 'display_post_states', array( $this, 'add_cartflows_post_state' ), 15, 1 ); |
| 49 |
|
| 50 |
add_filter( 'hidden_meta_boxes', array( $this, 'display_flow_slug_meta_box' ), 10, 2 ); |
| 51 |
|
| 52 |
/* View Post URL */ |
| 53 |
add_filter( 'post_row_actions', array( $this, 'post_row_actions' ), 10, 2 ); |
| 54 |
add_filter( 'preview_post_link', array( $this, 'preview_post_link' ), 10, 2 ); |
| 55 |
add_action( 'template_redirect', array( $this, 'redirect_to_step' ), 10 ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Add CartFlows post status. |
| 60 |
* |
| 61 |
* @param array $post_states post data. |
| 62 |
* @return array |
| 63 |
*/ |
| 64 |
public function add_cartflows_post_state( $post_states ) { |
| 65 |
|
| 66 |
global $post; |
| 67 |
|
| 68 |
if ( isset( $post->post_type ) && CARTFLOWS_STEP_POST_TYPE === $post->post_type ) { |
| 69 |
|
| 70 |
$flow_id = get_post_meta( $post->ID, 'wcf-flow-id', true ); |
| 71 |
$flow_title = get_the_title( $flow_id ); |
| 72 |
|
| 73 |
$post_states['cartflows_step'] = '( ' . __( 'Flow: ', 'cartflows' ) . $flow_id . ' | ' . __( 'Name: ', 'cartflows' ) . $flow_title . ')'; |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
return $post_states; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Display slugdiv. |
| 82 |
* |
| 83 |
* @param array $hidden metaboxes. |
| 84 |
* @param obj $screen screen. |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public function display_flow_slug_meta_box( $hidden, $screen ) { |
| 88 |
$post_type = $screen->id; |
| 89 |
if ( ! empty( $post_type ) && CARTFLOWS_FLOW_POST_TYPE === $post_type ) { |
| 90 |
$pos = array_search( 'slugdiv', $hidden, true ); |
| 91 |
unset( $hidden[ $pos ] ); |
| 92 |
} |
| 93 |
|
| 94 |
return $hidden; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Create custom post type |
| 99 |
*/ |
| 100 |
public function flow_post_type() { |
| 101 |
|
| 102 |
$labels = array( |
| 103 |
'name' => esc_html_x( 'Flows', 'flow general name', 'cartflows' ), |
| 104 |
'singular_name' => esc_html_x( 'Flow', 'flow singular name', 'cartflows' ), |
| 105 |
'search_items' => esc_html__( 'Search Flows', 'cartflows' ), |
| 106 |
'all_items' => esc_html__( 'All Flows', 'cartflows' ), |
| 107 |
'edit_item' => esc_html__( 'Edit Flow', 'cartflows' ), |
| 108 |
'view_item' => esc_html__( 'View Flow', 'cartflows' ), |
| 109 |
'add_new' => esc_html__( 'Add New', 'cartflows' ), |
| 110 |
'update_item' => esc_html__( 'Update Flow', 'cartflows' ), |
| 111 |
'add_new_item' => esc_html__( 'Add New', 'cartflows' ), |
| 112 |
'new_item_name' => esc_html__( 'New Flow Name', 'cartflows' ), |
| 113 |
); |
| 114 |
|
| 115 |
$args = array( |
| 116 |
'labels' => $labels, |
| 117 |
'show_in_menu' => false, |
| 118 |
'public' => false, // it's not public, not own permalink. |
| 119 |
'publicly_queryable' => false, // Prevent unauthenticated flow enumeration. |
| 120 |
'show_ui' => true, |
| 121 |
'query_var' => true, |
| 122 |
'can_export' => true, |
| 123 |
'show_in_admin_bar' => false, |
| 124 |
'exclude_from_search' => true, |
| 125 |
'has_archive' => false, // it shouldn't have archive page. |
| 126 |
'rewrite' => false, // it shouldn't have rewrite rules. |
| 127 |
'supports' => array( 'title', 'thumbnail', 'slug' ), |
| 128 |
'capability_type' => 'post', |
| 129 |
); |
| 130 |
|
| 131 |
if ( ! _is_cartflows_pro() ) { |
| 132 |
|
| 133 |
$flow_posts = get_posts( |
| 134 |
array( |
| 135 |
'posts_per_page' => 4, |
| 136 |
'post_type' => CARTFLOWS_FLOW_POST_TYPE, |
| 137 |
'post_status' => array( 'publish', 'pending', 'draft', 'future', 'private' ), |
| 138 |
) |
| 139 |
); |
| 140 |
|
| 141 |
if ( is_array( $flow_posts ) ) { |
| 142 |
|
| 143 |
$flow_count = count( $flow_posts ); |
| 144 |
|
| 145 |
if ( $flow_count > 3 || 3 === $flow_count ) { |
| 146 |
|
| 147 |
$args['capabilities'] = array( |
| 148 |
'create_posts' => 'do_not_allow', |
| 149 |
); |
| 150 |
$args['map_meta_cap'] = true; |
| 151 |
|
| 152 |
// Add new notice button. |
| 153 |
add_action( 'admin_print_footer_scripts', array( $this, 'add_new_notice_button' ) ); |
| 154 |
|
| 155 |
// Add the notice popup HTML to admin footer. |
| 156 |
add_action( 'admin_footer', array( $this, 'upgrade_to_pro_notice_popup' ) ); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
register_post_type( CARTFLOWS_FLOW_POST_TYPE, $args ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Show custom add new button. |
| 166 |
*/ |
| 167 |
public function add_new_notice_button() { |
| 168 |
|
| 169 |
$screen = get_current_screen(); |
| 170 |
|
| 171 |
if ( is_object( $screen ) && CARTFLOWS_FLOW_POST_TYPE === $screen->post_type && 'edit-cartflows_flow' === $screen->id ) { |
| 172 |
?> |
| 173 |
<script> |
| 174 |
jQuery('.wrap h1.wp-heading-inline').after('<a type="button" class="wcf-custom-add-new-button button">Add New</a>'); |
| 175 |
</script> |
| 176 |
<?php |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Upgrade to pro notice popup. |
| 182 |
* |
| 183 |
* @since 1.3.4 |
| 184 |
* |
| 185 |
* @return void |
| 186 |
*/ |
| 187 |
public function upgrade_to_pro_notice_popup() { |
| 188 |
|
| 189 |
?> |
| 190 |
<div id="cartflows-upgrade-notice-overlay" style="display:none;"></div> |
| 191 |
<div id="cartflows-upgrade-notice-popup" style="display:none;"> |
| 192 |
<div class="inner"> |
| 193 |
<div class="heading"> |
| 194 |
<span><?php esc_html_e( 'Upgrade to CartFlows Pro', 'cartflows' ); ?></span> |
| 195 |
<span class="cartflows-close-popup-button tb-close-icon"></span> |
| 196 |
</div> |
| 197 |
<div class="contents"> |
| 198 |
<div class="wcf-notice"> |
| 199 |
<p>Upgrade to CartFlows Pro for adding more flows and other features. <a href ="<?php echo esc_url( \Cartflows_Helper::get_upgrade_to_pro_link( '', 'https://cartflows.com/' ) ); ?>" target="_blank"> Click here</a> to upgrade.</p> |
| 200 |
</div> |
| 201 |
</div> |
| 202 |
</div> |
| 203 |
</div> |
| 204 |
<?php |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Change metabox position. |
| 209 |
*/ |
| 210 |
public function wcf_change_metabox_position() { |
| 211 |
|
| 212 |
remove_meta_box( 'slugdiv', CARTFLOWS_FLOW_POST_TYPE, 'normal' ); |
| 213 |
add_meta_box( 'slugdiv', __( 'Slug', 'cartflows' ), 'post_slug_meta_box', CARTFLOWS_FLOW_POST_TYPE, 'side', 'high' ); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Add post raw actions |
| 218 |
* |
| 219 |
* @param array $actions actions. |
| 220 |
* @param array $post post data. |
| 221 |
* @return array |
| 222 |
*/ |
| 223 |
public function post_row_actions( $actions, $post ) { |
| 224 |
|
| 225 |
$first_step = $this->get_first_step_url( $post ); |
| 226 |
|
| 227 |
if ( $first_step && isset( $actions['view'] ) ) { |
| 228 |
|
| 229 |
$actions['view'] = '<a href="' . $first_step . '">' . __( 'View', 'cartflows' ) . '</a>'; |
| 230 |
} |
| 231 |
|
| 232 |
return $actions; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Returns previous post link |
| 237 |
* |
| 238 |
* @param string $prev_link previous link. |
| 239 |
* @param array $post post data. |
| 240 |
* @return string |
| 241 |
*/ |
| 242 |
public function preview_post_link( $prev_link, $post ) { |
| 243 |
|
| 244 |
if ( $this->is_flow_post_type( $post ) ) { |
| 245 |
|
| 246 |
$first_step = $this->get_first_step_url( $post ); |
| 247 |
|
| 248 |
if ( $first_step ) { |
| 249 |
|
| 250 |
return $first_step; |
| 251 |
} |
| 252 |
|
| 253 |
return ''; |
| 254 |
} |
| 255 |
|
| 256 |
return $prev_link; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Check if post type is flow |
| 261 |
* |
| 262 |
* @param array $post post data. |
| 263 |
* @return bool |
| 264 |
*/ |
| 265 |
public function is_flow_post_type( $post ) { |
| 266 |
|
| 267 |
if ( isset( $post ) && CARTFLOWS_FLOW_POST_TYPE === $post->post_type ) { |
| 268 |
|
| 269 |
return true; |
| 270 |
} |
| 271 |
|
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Redirect to first step |
| 277 |
* |
| 278 |
* @return void |
| 279 |
*/ |
| 280 |
public function redirect_to_step() { |
| 281 |
|
| 282 |
global $post; |
| 283 |
|
| 284 |
$first_step = $this->get_first_step_url( $post ); |
| 285 |
|
| 286 |
if ( $first_step ) { |
| 287 |
|
| 288 |
wp_safe_redirect( $first_step ); |
| 289 |
exit; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Return first step URL |
| 295 |
* |
| 296 |
* @param array $post post data. |
| 297 |
* @return bool |
| 298 |
*/ |
| 299 |
public function get_first_step_url( $post ) { |
| 300 |
|
| 301 |
if ( $this->is_flow_post_type( $post ) ) { |
| 302 |
|
| 303 |
$flow_id = $post->ID; |
| 304 |
$title = $post->post_title; |
| 305 |
$steps = get_post_meta( $flow_id, 'wcf-steps', true ); |
| 306 |
|
| 307 |
if ( is_array( $steps ) && ! empty( $steps ) && isset( $steps[0]['id'] ) ) { |
| 308 |
|
| 309 |
return get_permalink( $steps[0]['id'] ); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Add Update messages for any custom post type |
| 318 |
* |
| 319 |
* @param array $messages Array of default messages. |
| 320 |
*/ |
| 321 |
public function custom_post_type_post_update_messages( $messages ) { |
| 322 |
|
| 323 |
$custom_post_type = get_post_type( get_the_ID() ); |
| 324 |
|
| 325 |
if ( CARTFLOWS_FLOW_POST_TYPE == $custom_post_type ) { |
| 326 |
|
| 327 |
$obj = get_post_type_object( $custom_post_type ); |
| 328 |
$singular_name = $obj->labels->singular_name; |
| 329 |
$messages[ $custom_post_type ] = array( |
| 330 |
0 => '', // Unused. Messages start at index 1. |
| 331 |
/* translators: %s: singular custom post type name */ |
| 332 |
1 => sprintf( __( '%s updated.', 'cartflows' ), $singular_name ), |
| 333 |
/* translators: %s: singular custom post type name */ |
| 334 |
2 => sprintf( __( 'Custom %s updated.', 'cartflows' ), $singular_name ), |
| 335 |
/* translators: %s: singular custom post type name */ |
| 336 |
3 => sprintf( __( 'Custom %s deleted.', 'cartflows' ), $singular_name ), |
| 337 |
/* translators: %s: singular custom post type name */ |
| 338 |
4 => sprintf( __( '%s updated.', 'cartflows' ), $singular_name ), |
| 339 |
/* translators: %1$s: singular custom post type name ,%2$s: date and time of the revision */ |
| 340 |
5 => isset( $_GET['revision'] ) ? sprintf( __( '%1$s restored to revision from %2$s', 'cartflows' ), $singular_name, wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 341 |
/* translators: %s: singular custom post type name */ |
| 342 |
6 => sprintf( __( '%s published.', 'cartflows' ), $singular_name ), |
| 343 |
/* translators: %s: singular custom post type name */ |
| 344 |
7 => sprintf( __( '%s saved.', 'cartflows' ), $singular_name ), |
| 345 |
/* translators: %s: singular custom post type name */ |
| 346 |
8 => sprintf( __( '%s submitted.', 'cartflows' ), $singular_name ), |
| 347 |
/* translators: %s: singular custom post type name */ |
| 348 |
9 => sprintf( __( '%s scheduled for.', 'cartflows' ), $singular_name ), |
| 349 |
/* translators: %s: singular custom post type name */ |
| 350 |
10 => sprintf( __( '%s draft updated.', 'cartflows' ), $singular_name ), |
| 351 |
); |
| 352 |
} |
| 353 |
|
| 354 |
return $messages; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* Kicking this off by calling 'get_instance()' method |
| 360 |
*/ |
| 361 |
Cartflows_Flow_Post_Type::get_instance(); |
| 362 |
|