| 1 |
<?php |
| 2 |
/** |
| 3 |
* Flow post type |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Initialization |
| 10 |
* |
| 11 |
* @since 1.0.0 |
| 12 |
*/ |
| 13 |
class Cartflows_Flow_Post_Type { |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* Member Variable |
| 18 |
* |
| 19 |
* @var instance |
| 20 |
*/ |
| 21 |
private static $instance; |
| 22 |
|
| 23 |
/** |
| 24 |
* Initiator |
| 25 |
*/ |
| 26 |
public static function get_instance() { |
| 27 |
if ( ! isset( self::$instance ) ) { |
| 28 |
self::$instance = new self; |
| 29 |
} |
| 30 |
return self::$instance; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
|
| 38 |
add_action( 'init', array( $this, 'flow_post_type' ) ); |
| 39 |
add_action( 'admin_menu', array( $this, 'register_as_submenu' ), 100 ); |
| 40 |
|
| 41 |
add_filter( 'post_updated_messages', array( $this, 'custom_post_type_post_update_messages' ) ); |
| 42 |
|
| 43 |
/* View Post URL */ |
| 44 |
add_filter( 'post_row_actions', array( $this, 'post_row_actions' ), 10, 2 ); |
| 45 |
add_filter( 'preview_post_link', array( $this, 'preview_post_link' ), 10, 2 ); |
| 46 |
add_action( 'template_redirect', array( $this, 'redirect_to_step' ), 10 ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Create custom post type |
| 51 |
*/ |
| 52 |
function flow_post_type() { |
| 53 |
|
| 54 |
$labels = array( |
| 55 |
'name' => esc_html_x( 'Flows', 'flow general name', 'cartflows' ), |
| 56 |
'singular_name' => esc_html_x( 'Flow', 'flow singular name', 'cartflows' ), |
| 57 |
'search_items' => esc_html__( 'Search Flows', 'cartflows' ), |
| 58 |
'all_items' => esc_html__( 'All Flows', 'cartflows' ), |
| 59 |
'edit_item' => esc_html__( 'Edit Flow', 'cartflows' ), |
| 60 |
'view_item' => esc_html__( 'View Flow', 'cartflows' ), |
| 61 |
'add_new' => esc_html__( 'Add New', 'cartflows' ), |
| 62 |
'update_item' => esc_html__( 'Update Flow', 'cartflows' ), |
| 63 |
'add_new_item' => esc_html__( 'Add New', 'cartflows' ), |
| 64 |
'new_item_name' => esc_html__( 'New Flow Name', 'cartflows' ), |
| 65 |
); |
| 66 |
|
| 67 |
$args = array( |
| 68 |
'labels' => $labels, |
| 69 |
'show_in_menu' => false, |
| 70 |
'public' => false, // it's not public, not own permalink. |
| 71 |
'publicly_queryable' => true, // you should be able to query it. |
| 72 |
'show_ui' => true, |
| 73 |
'query_var' => true, |
| 74 |
'can_export' => true, |
| 75 |
'show_in_admin_bar' => true, |
| 76 |
'exclude_from_search' => true, |
| 77 |
'has_archive' => false, // it shouldn't have archive page. |
| 78 |
'rewrite' => false, // it shouldn't have rewrite rules. |
| 79 |
'supports' => array( 'title' ), |
| 80 |
'capability_type' => 'post', |
| 81 |
); |
| 82 |
|
| 83 |
if ( false && ! _is_cartflows_pro() ) { |
| 84 |
|
| 85 |
$flow_posts = get_posts( |
| 86 |
array( |
| 87 |
'posts_per_page' => 4, |
| 88 |
'post_type' => CARTFLOWS_FLOW_POST_TYPE, |
| 89 |
'post_status' => array( 'publish', 'pending', 'draft', 'future', 'private', 'inherit', 'trash' ), |
| 90 |
) |
| 91 |
); |
| 92 |
|
| 93 |
if ( is_array( $flow_posts ) ) { |
| 94 |
|
| 95 |
$flow_count = count( $flow_posts ); |
| 96 |
|
| 97 |
if ( $flow_count > 3 || 3 === $flow_count ) { |
| 98 |
|
| 99 |
$args['capabilities'] = array( |
| 100 |
'create_posts' => 'do_not_allow', |
| 101 |
); |
| 102 |
$args['map_meta_cap'] = true; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
register_post_type( CARTFLOWS_FLOW_POST_TYPE, $args ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Add post raw actions |
| 112 |
* |
| 113 |
* @param array $actions actions. |
| 114 |
* @param array $post post data. |
| 115 |
* @return array |
| 116 |
*/ |
| 117 |
function post_row_actions( $actions, $post ) { |
| 118 |
|
| 119 |
$first_step = $this->get_first_step_url( $post ); |
| 120 |
|
| 121 |
if ( $first_step && isset( $actions['view'] ) ) { |
| 122 |
|
| 123 |
$actions['view'] = '<a href="' . $first_step . '">' . __( 'View', 'cartflows' ) . '</a>'; |
| 124 |
} |
| 125 |
|
| 126 |
return $actions; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Returns previous post link |
| 131 |
* |
| 132 |
* @param string $prev_link previous link. |
| 133 |
* @param array $post post data. |
| 134 |
* @return string |
| 135 |
*/ |
| 136 |
function preview_post_link( $prev_link, $post ) { |
| 137 |
|
| 138 |
if ( $this->is_flow_post_type( $post ) ) { |
| 139 |
|
| 140 |
$first_step = $this->get_first_step_url( $post ); |
| 141 |
|
| 142 |
if ( $first_step ) { |
| 143 |
|
| 144 |
return $first_step; |
| 145 |
} |
| 146 |
|
| 147 |
return ''; |
| 148 |
} |
| 149 |
|
| 150 |
return $prev_link; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Check if post type is flow |
| 155 |
* |
| 156 |
* @param array $post post data. |
| 157 |
* @return bool |
| 158 |
*/ |
| 159 |
function is_flow_post_type( $post ) { |
| 160 |
|
| 161 |
if ( isset( $post ) && CARTFLOWS_FLOW_POST_TYPE === $post->post_type ) { |
| 162 |
|
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Redirect to first step |
| 171 |
* |
| 172 |
* @return void |
| 173 |
*/ |
| 174 |
function redirect_to_step() { |
| 175 |
|
| 176 |
global $post; |
| 177 |
|
| 178 |
$first_step = $this->get_first_step_url( $post ); |
| 179 |
|
| 180 |
if ( $first_step ) { |
| 181 |
|
| 182 |
wp_redirect( $first_step ); |
| 183 |
die; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Return first step URL |
| 189 |
* |
| 190 |
* @param array $post post data. |
| 191 |
* @return bool |
| 192 |
*/ |
| 193 |
function get_first_step_url( $post ) { |
| 194 |
|
| 195 |
if ( $this->is_flow_post_type( $post ) ) { |
| 196 |
|
| 197 |
$flow_id = $post->ID; |
| 198 |
$title = $post->post_title; |
| 199 |
$steps = get_post_meta( $flow_id, 'wcf-steps', true ); |
| 200 |
|
| 201 |
if ( is_array( $steps ) && ! empty( $steps ) && isset( $steps[0]['id'] ) ) { |
| 202 |
|
| 203 |
return get_permalink( $steps[0]['id'] ); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Register the admin menu for Custom Layouts |
| 212 |
* |
| 213 |
* @since 1.0.0 |
| 214 |
* Moved the menu under Appearance -> Custom Layouts |
| 215 |
public function register_admin_menu() { |
| 216 |
add_submenu_page( |
| 217 |
CARTFLOWS_SLUG, |
| 218 |
__( 'Flows', 'wcf' ), |
| 219 |
__( 'Flows', 'wcf' ), |
| 220 |
'edit_pages', |
| 221 |
'edit.php?post_type=' . CARTFLOWS_FLOW_POST_TYPE |
| 222 |
); |
| 223 |
} |
| 224 |
*/ |
| 225 |
public function register_as_submenu() { |
| 226 |
|
| 227 |
global $submenu; |
| 228 |
|
| 229 |
$submenu[ CARTFLOWS_SLUG ][0] = array( |
| 230 |
__( 'Flows', 'cartflows' ), |
| 231 |
'edit_pages', |
| 232 |
'edit.php?post_type=' . CARTFLOWS_FLOW_POST_TYPE, |
| 233 |
); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Add Update messages for any custom post type |
| 238 |
* |
| 239 |
* @param array $messages Array of default messages. |
| 240 |
*/ |
| 241 |
function custom_post_type_post_update_messages( $messages ) { |
| 242 |
|
| 243 |
$custom_post_type = get_post_type( get_the_ID() ); |
| 244 |
|
| 245 |
if ( CARTFLOWS_FLOW_POST_TYPE == $custom_post_type ) { |
| 246 |
|
| 247 |
$obj = get_post_type_object( $custom_post_type ); |
| 248 |
$singular_name = $obj->labels->singular_name; |
| 249 |
$messages[ $custom_post_type ] = array( |
| 250 |
0 => '', // Unused. Messages start at index 1. |
| 251 |
/* translators: %s: singular custom post type name */ |
| 252 |
1 => sprintf( __( '%s updated.', 'cartflows' ), $singular_name ), |
| 253 |
/* translators: %s: singular custom post type name */ |
| 254 |
2 => sprintf( __( 'Custom %s updated.', 'cartflows' ), $singular_name ), |
| 255 |
/* translators: %s: singular custom post type name */ |
| 256 |
3 => sprintf( __( 'Custom %s deleted.', 'cartflows' ), $singular_name ), |
| 257 |
/* translators: %s: singular custom post type name */ |
| 258 |
4 => sprintf( __( '%s updated.', 'cartflows' ), $singular_name ), |
| 259 |
/* translators: %1$s: singular custom post type name ,%2$s: date and time of the revision */ |
| 260 |
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, |
| 261 |
/* translators: %s: singular custom post type name */ |
| 262 |
6 => sprintf( __( '%s published.', 'cartflows' ), $singular_name ), |
| 263 |
/* translators: %s: singular custom post type name */ |
| 264 |
7 => sprintf( __( '%s saved.', 'cartflows' ), $singular_name ), |
| 265 |
/* translators: %s: singular custom post type name */ |
| 266 |
8 => sprintf( __( '%s submitted.', 'cartflows' ), $singular_name ), |
| 267 |
/* translators: %s: singular custom post type name */ |
| 268 |
9 => sprintf( __( '%s scheduled for.', 'cartflows' ), $singular_name ), |
| 269 |
/* translators: %s: singular custom post type name */ |
| 270 |
10 => sprintf( __( '%s draft updated.', 'cartflows' ), $singular_name ), |
| 271 |
); |
| 272 |
} |
| 273 |
|
| 274 |
return $messages; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Kicking this off by calling 'get_instance()' method |
| 280 |
*/ |
| 281 |
Cartflows_Flow_Post_Type::get_instance(); |
| 282 |
|