Complex classes like Pods_Pages often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Pods_Pages, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Pods_Pages extends PodsComponent { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Current Pod Page |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | * |
||
| 33 | * @since 2.0 |
||
| 34 | */ |
||
| 35 | public static $exists = null; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Object type |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | * |
||
| 42 | * @since 2.0 |
||
| 43 | */ |
||
| 44 | private $object_type = '_pods_page'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Whether the page has been checked already |
||
| 48 | * |
||
| 49 | * @var bool |
||
| 50 | * |
||
| 51 | * @since 2.1 |
||
| 52 | */ |
||
| 53 | public static $checked = false; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Keep track of if pods_content has been called yet |
||
| 57 | * |
||
| 58 | * @var bool |
||
| 59 | * |
||
| 60 | * @since 2.3 |
||
| 61 | */ |
||
| 62 | public static $content_called = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The capability type. |
||
| 66 | * |
||
| 67 | * @link https://codex.wordpress.org/Function_Reference/register_post_type |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | private $capability_type = 'pods_page'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Do things like register/enqueue scripts and stylesheets |
||
| 74 | * |
||
| 75 | * @since 2.0 |
||
| 76 | */ |
||
| 77 | public function __construct() { |
||
| 78 | |||
| 79 | add_shortcode( 'pods-content', array( $this, 'shortcode' ) ); |
||
| 80 | |||
| 81 | $args = array( |
||
| 82 | 'label' => 'Pod Pages', |
||
| 83 | 'labels' => array( 'singular_name' => 'Pod Page' ), |
||
| 84 | 'public' => false, |
||
| 85 | 'can_export' => false, |
||
| 86 | 'show_ui' => true, |
||
| 87 | 'show_in_menu' => false, |
||
| 88 | 'query_var' => false, |
||
| 89 | 'rewrite' => false, |
||
| 90 | 'has_archive' => false, |
||
| 91 | 'hierarchical' => false, |
||
| 92 | 'supports' => array( 'title', 'author', 'revisions' ), |
||
| 93 | 'menu_icon' => 'dashicons-pods', |
||
| 94 | ); |
||
| 95 | |||
| 96 | if ( ! pods_is_admin() ) { |
||
| 97 | $args['capability_type'] = $this->capability_type; |
||
| 98 | } |
||
| 99 | |||
| 100 | $args = PodsInit::object_label_fix( $args, 'post_type' ); |
||
| 101 | |||
| 102 | register_post_type( $this->object_type, apply_filters( 'pods_internal_register_post_type_object_page', $args ) ); |
||
| 103 | |||
| 104 | add_filter( 'post_type_link', array( $this, 'post_type_link' ), 10, 2 ); |
||
| 105 | |||
| 106 | if ( ! is_admin() ) { |
||
| 107 | add_action( 'load_textdomain', array( $this, 'page_check' ), 12 ); |
||
| 108 | } else { |
||
| 109 | add_filter( 'post_updated_messages', array( $this, 'setup_updated_messages' ), 10, 1 ); |
||
| 110 | |||
| 111 | add_action( 'dbx_post_advanced', array( $this, 'edit_page_form' ) ); |
||
| 112 | |||
| 113 | add_action( 'pods_meta_groups', array( $this, 'add_meta_boxes' ) ); |
||
| 114 | add_filter( 'get_post_metadata', array( $this, 'get_meta' ), 10, 4 ); |
||
| 115 | add_filter( 'update_post_metadata', array( $this, 'save_meta' ), 10, 4 ); |
||
| 116 | |||
| 117 | add_action( 'pods_meta_save_pre_post__pods_page', array( $this, 'fix_filters' ), 10, 5 ); |
||
| 118 | add_action( 'post_updated', array( $this, 'clear_cache' ), 10, 3 ); |
||
| 119 | add_action( 'delete_post', array( $this, 'clear_cache' ), 10, 1 ); |
||
| 120 | add_filter( 'post_row_actions', array( $this, 'remove_row_actions' ), 10, 2 ); |
||
| 121 | add_filter( 'bulk_actions-edit-' . $this->object_type, array( $this, 'remove_bulk_actions' ) ); |
||
| 122 | |||
| 123 | add_filter( 'builder_layout_filter_non_layout_post_types', array( $this, 'disable_builder_layout' ) ); |
||
| 124 | } |
||
| 125 | |||
| 126 | add_filter( 'members_get_capabilities', array( $this, 'get_capabilities' ) ); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param $caps |
||
| 131 | * |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | public function get_capabilities( $caps ) { |
||
| 135 | |||
| 136 | $caps = array_merge( |
||
| 137 | $caps, array( |
||
| 138 | 'edit_' . $this->capability_type, |
||
| 139 | 'read_' . $this->capability_type, |
||
| 140 | 'delete_' . $this->capability_type, |
||
| 141 | 'edit_' . $this->capability_type . 's', |
||
| 142 | 'edit_others_' . $this->capability_type . 's', |
||
| 143 | 'publish_' . $this->capability_type . 's', |
||
| 144 | 'read_private_' . $this->capability_type . 's', |
||
| 145 | 'edit_' . $this->capability_type . 's', |
||
| 146 | ) |
||
| 147 | ); |
||
| 148 | |||
| 149 | return $caps; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Pod Page Content Shortcode support for use anywhere that supports WP Shortcodes |
||
| 154 | * |
||
| 155 | * @param array $tags An associative array of shortcode properties |
||
| 156 | * @param string $content Not currently used |
||
| 157 | * |
||
| 158 | * @return string |
||
| 159 | * @since 2.3.9 |
||
| 160 | */ |
||
| 161 | public function shortcode( $tags, $content = null ) { |
||
| 162 | |||
| 163 | if ( ! isset( $tags['page'] ) || empty( $tags['page'] ) ) { |
||
| 164 | $tags['page'] = null; |
||
| 165 | } |
||
| 166 | |||
| 167 | $pods_page = self::exists( $tags['page'] ); |
||
| 168 | |||
| 169 | if ( empty( $pods_page ) ) { |
||
| 170 | return '<p>Pods Page not found</p>'; |
||
| 171 | } |
||
| 172 | |||
| 173 | return self::content( true, $pods_page ); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Disable this Post Type from appearing in the Builder layouts list |
||
| 178 | * |
||
| 179 | * @param array $post_types |
||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | public function disable_builder_layout( $post_types ) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Update Post Type messages |
||
| 192 | * |
||
| 193 | * @param array $messages |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | * @since 2.0.2 |
||
| 197 | */ |
||
| 198 | public function setup_updated_messages( $messages ) { |
||
| 199 | |||
| 200 | global $post, $post_ID; |
||
| 201 | |||
| 202 | $post_type = get_post_type_object( $this->object_type ); |
||
| 203 | |||
| 204 | $labels = $post_type->labels; |
||
| 205 | |||
| 206 | $messages[ $post_type->name ] = array( |
||
| 207 | 1 => sprintf( __( '%1$s updated. <a href="%2$s">%3$s</a>', 'pods' ), $labels->singular_name, esc_url( get_permalink( $post_ID ) ), $labels->view_item ), |
||
| 208 | 2 => __( 'Custom field updated.', 'pods' ), |
||
| 209 | 3 => __( 'Custom field deleted.', 'pods' ), |
||
| 210 | 4 => sprintf( __( '%s updated.', 'pods' ), $labels->singular_name ), |
||
| 211 | /* translators: %s: date and time of the revision */ |
||
| 212 | 5 => isset( $_GET['revision'] ) ? sprintf( __( '%1$s restored to revision from %2$s', 'pods' ), $labels->singular_name, wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, |
||
| 213 | 6 => sprintf( __( '%1$s published. <a href="%2$s">%3$s</a>', 'pods' ), $labels->singular_name, esc_url( get_permalink( $post_ID ) ), $labels->view_item ), |
||
| 214 | 7 => sprintf( __( '%s saved.', 'pods' ), $labels->singular_name ), |
||
| 215 | 8 => sprintf( __( '%1$s submitted. <a target="_blank" href="%2$s">Preview %3$s</a>', 'pods' ), $labels->singular_name, esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ), $labels->singular_name ), |
||
| 216 | 9 => sprintf( |
||
| 217 | __( '%s scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview %s</a>', 'pods' ), $labels->singular_name, |
||
| 218 | // translators: Publish box date format, see http://php.net/date |
||
| 219 | date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink( $post_ID ) ), $labels->singular_name |
||
| 220 | ), |
||
| 221 | 10 => sprintf( __( '%1$s draft updated. <a target="_blank" href="%2$s">Preview %3$s</a>', 'pods' ), $labels->singular_name, esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ), $labels->singular_name ), |
||
| 222 | ); |
||
| 223 | |||
| 224 | if ( false === (boolean) $post_type->public ) { |
||
| 225 | $messages[ $post_type->name ][1] = sprintf( __( '%s updated.', 'pods' ), $labels->singular_name ); |
||
| 226 | $messages[ $post_type->name ][6] = sprintf( __( '%s published.', 'pods' ), $labels->singular_name ); |
||
| 227 | $messages[ $post_type->name ][8] = sprintf( __( '%s submitted.', 'pods' ), $labels->singular_name ); |
||
| 228 | $messages[ $post_type->name ][9] = sprintf( |
||
| 229 | __( '%s scheduled for: <strong>%1$s</strong>.', 'pods' ), $labels->singular_name, |
||
| 230 | // translators: Publish box date format, see http://php.net/date |
||
| 231 | date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ) |
||
| 232 | ); |
||
| 233 | $messages[ $post_type->name ][10] = sprintf( __( '%s draft updated.', 'pods' ), $labels->singular_name ); |
||
| 234 | } |
||
| 235 | |||
| 236 | return $messages; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Enqueue styles |
||
| 241 | * |
||
| 242 | * @since 2.0 |
||
| 243 | */ |
||
| 244 | public function admin_assets() { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Fix filters, specifically removing balanceTags |
||
| 251 | * |
||
| 252 | * @since 2.0.1 |
||
| 253 | * |
||
| 254 | * @param $data |
||
| 255 | * @param null $pod |
||
| 256 | * @param null $id |
||
| 257 | * @param null $groups |
||
| 258 | * @param null $post |
||
| 259 | */ |
||
| 260 | public function fix_filters( $data, $pod = null, $id = null, $groups = null, $post = null ) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Remove unused row actions |
||
| 267 | * |
||
| 268 | * @since 2.0.5 |
||
| 269 | * |
||
| 270 | * @param $actions |
||
| 271 | * @param $post |
||
| 272 | * |
||
| 273 | * @return |
||
| 274 | */ |
||
| 275 | public function remove_row_actions( $actions, $post ) { |
||
| 276 | |||
| 277 | global $current_screen; |
||
| 278 | |||
| 279 | if ( ! is_object( $current_screen ) || $this->object_type != $current_screen->post_type ) { |
||
| 280 | return $actions; |
||
| 281 | } |
||
| 282 | |||
| 283 | if ( isset( $actions['view'] ) ) { |
||
| 284 | unset( $actions['view'] ); |
||
| 285 | } |
||
| 286 | |||
| 287 | if ( isset( $actions['inline hide-if-no-js'] ) ) { |
||
| 288 | unset( $actions['inline hide-if-no-js'] ); |
||
| 289 | } |
||
| 290 | |||
| 291 | // W3 Total Cache |
||
| 292 | if ( isset( $actions['pgcache_purge'] ) ) { |
||
| 293 | unset( $actions['pgcache_purge'] ); |
||
| 294 | } |
||
| 295 | |||
| 296 | return $actions; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Remove unused bulk actions |
||
| 301 | * |
||
| 302 | * @since 2.0.5 |
||
| 303 | * |
||
| 304 | * @param $actions |
||
| 305 | * |
||
| 306 | * @return |
||
| 307 | */ |
||
| 308 | public function remove_bulk_actions( $actions ) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Clear cache on save |
||
| 319 | * |
||
| 320 | * @since 2.0 |
||
| 321 | * |
||
| 322 | * @param $data |
||
| 323 | * @param null $pod |
||
| 324 | * @param null $id |
||
| 325 | * @param null $groups |
||
| 326 | * @param null $post |
||
| 327 | */ |
||
| 328 | public function clear_cache( $data, $pod = null, $id = null, $groups = null, $post = null ) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Change post title placeholder text |
||
| 360 | * |
||
| 361 | * @since 2.0 |
||
| 362 | * |
||
| 363 | * @param $text |
||
| 364 | * @param $post |
||
| 365 | * |
||
| 366 | * @return string|void |
||
| 367 | */ |
||
| 368 | public function set_title_text( $text, $post ) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Edit page form |
||
| 375 | * |
||
| 376 | * @since 2.0 |
||
| 377 | */ |
||
| 378 | public function edit_page_form() { |
||
| 379 | |||
| 380 | global $post_type; |
||
| 381 | |||
| 382 | if ( $this->object_type != $post_type ) { |
||
| 383 | return; |
||
| 384 | } |
||
| 385 | |||
| 386 | add_filter( 'enter_title_here', array( $this, 'set_title_text' ), 10, 2 ); |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Filter permalinks and adjust for pod pages |
||
| 391 | * |
||
| 392 | * @param $post_link |
||
| 393 | * @param $post |
||
| 394 | * |
||
| 395 | * @return mixed |
||
| 396 | */ |
||
| 397 | public function post_type_link( $post_link, $post ) { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Add meta boxes to the page |
||
| 414 | * |
||
| 415 | * @since 2.0 |
||
| 416 | */ |
||
| 417 | public function add_meta_boxes() { |
||
| 418 | |||
| 419 | $pod = array( |
||
| 420 | 'name' => $this->object_type, |
||
| 421 | 'type' => 'post_type', |
||
| 422 | ); |
||
| 423 | |||
| 424 | if ( isset( PodsMeta::$post_types[ $pod['name'] ] ) ) { |
||
| 425 | return; |
||
| 426 | } |
||
| 427 | |||
| 428 | if ( ! function_exists( 'get_page_templates' ) ) { |
||
| 429 | include_once ABSPATH . 'wp-admin/includes/theme.php'; |
||
| 430 | } |
||
| 431 | |||
| 432 | $page_templates = apply_filters( 'pods_page_templates', get_page_templates() ); |
||
| 433 | |||
| 434 | $page_templates[ __( '-- Page Template --', 'pods' ) ] = ''; |
||
| 435 | |||
| 436 | $page_templates[ __( 'Custom (uses only Pod Page content)', 'pods' ) ] = '_custom'; |
||
| 437 | |||
| 438 | if ( ! in_array( 'pods.php', $page_templates ) && locate_template( array( 'pods.php', false ) ) ) { |
||
| 439 | $page_templates[ __( 'Pods (Pods Default)', 'pods' ) ] = 'pods.php'; |
||
| 440 | } |
||
| 441 | |||
| 442 | if ( ! in_array( 'page.php', $page_templates ) && locate_template( array( 'page.php', false ) ) ) { |
||
| 443 | $page_templates[ __( 'Page (WP Default)', 'pods' ) ] = 'page.php'; |
||
| 444 | } |
||
| 445 | |||
| 446 | if ( ! in_array( 'index.php', $page_templates ) && locate_template( array( 'index.php', false ) ) ) { |
||
| 447 | $page_templates[ __( 'Index (WP Fallback)', 'pods' ) ] = 'index.php'; |
||
| 448 | } |
||
| 449 | |||
| 450 | ksort( $page_templates ); |
||
| 451 | |||
| 452 | $page_templates = array_flip( $page_templates ); |
||
| 453 | |||
| 454 | $fields = array( |
||
| 455 | array( |
||
| 456 | 'name' => 'page_title', |
||
| 457 | 'label' => __( 'Page Title', 'pods' ), |
||
| 458 | 'type' => 'text', |
||
| 459 | ), |
||
| 460 | array( |
||
| 461 | 'name' => 'code', |
||
| 462 | 'label' => __( 'Page Code', 'pods' ), |
||
| 463 | 'type' => 'code', |
||
| 464 | 'attributes' => array( |
||
| 465 | 'id' => 'content', |
||
| 466 | ), |
||
| 467 | 'label_options' => array( |
||
| 468 | 'attributes' => array( |
||
| 469 | 'for' => 'content', |
||
| 470 | ), |
||
| 471 | ), |
||
| 472 | ), |
||
| 473 | array( |
||
| 474 | 'name' => 'precode', |
||
| 475 | 'label' => __( 'Page Precode', 'pods' ), |
||
| 476 | 'type' => 'code', |
||
| 477 | 'help' => __( 'Precode will run before your theme outputs the page. It is expected that this value will be a block of PHP. You must open the PHP tag here, as we do not open it for you by default.', 'pods' ), |
||
| 478 | ), |
||
| 479 | array( |
||
| 480 | 'name' => 'page_template', |
||
| 481 | 'label' => __( 'Page Template', 'pods' ), |
||
| 482 | 'type' => 'pick', |
||
| 483 | 'data' => $page_templates, |
||
| 484 | ), |
||
| 485 | ); |
||
| 486 | |||
| 487 | pods_group_add( $pod, __( 'Page', 'pods' ), $fields, 'normal', 'high' ); |
||
| 488 | |||
| 489 | $associated_pods = array( |
||
| 490 | 0 => __( '-- Select a Pod --', 'pods' ), |
||
| 491 | ); |
||
| 492 | |||
| 493 | $all_pods = pods_api()->load_pods( array( 'names' => true ) ); |
||
| 494 | |||
| 495 | if ( ! empty( $all_pods ) ) { |
||
| 496 | foreach ( $all_pods as $pod_name => $pod_label ) { |
||
| 497 | $associated_pods[ $pod_name ] = $pod_label . ' (' . $pod_name . ')'; |
||
| 498 | } |
||
| 499 | } else { |
||
| 500 | $associated_pods[0] = __( 'None Found', 'pods' ); |
||
| 501 | } |
||
| 502 | |||
| 503 | $fields = array( |
||
| 504 | array( |
||
| 505 | 'name' => 'pod', |
||
| 506 | 'label' => __( 'Associated Pod', 'pods' ), |
||
| 507 | 'default' => 0, |
||
| 508 | 'type' => 'pick', |
||
| 509 | 'data' => $associated_pods, |
||
| 510 | 'dependency' => true, |
||
| 511 | ), |
||
| 512 | array( |
||
| 513 | 'name' => 'pod_slug', |
||
| 514 | 'label' => __( 'Wildcard Slug', 'pods' ), |
||
| 515 | 'help' => __( 'Setting the Wildcard Slug is an easy way to setup a detail page. You can use the special tag {@url.2} to match the *third* level of the URL of a Pod Page named "first/second/*" part of the pod page. This is functionally the same as using pods_v_sanitized( 2, "url" ) in PHP.', 'pods' ), |
||
| 516 | 'type' => 'text', |
||
| 517 | 'excludes-on' => array( 'pod' => 0 ), |
||
| 518 | ), |
||
| 519 | ); |
||
| 520 | |||
| 521 | pods_group_add( $pod, __( 'Pod Association', 'pods' ), $fields, 'normal', 'high' ); |
||
| 522 | |||
| 523 | $fields = array( |
||
| 524 | array( |
||
| 525 | 'name' => 'admin_only', |
||
| 526 | 'label' => __( 'Restrict access to Admins?', 'pods' ), |
||
| 527 | 'default' => 0, |
||
| 528 | 'type' => 'boolean', |
||
| 529 | 'dependency' => true, |
||
| 530 | ), |
||
| 531 | array( |
||
| 532 | 'name' => 'restrict_role', |
||
| 533 | 'label' => __( 'Restrict access by Role?', 'pods' ), |
||
| 534 | 'help' => array( |
||
| 535 | __( '<h6>Roles</h6> Roles are assigned to users to provide them access to specific functionality in WordPress. Please see the Roles and Capabilities component in Pods for an easy tool to add your own roles and edit existing ones.', 'pods' ), |
||
| 536 | 'http://codex.wordpress.org/Roles_and_Capabilities', |
||
| 537 | ), |
||
| 538 | 'default' => 0, |
||
| 539 | 'type' => 'boolean', |
||
| 540 | 'dependency' => true, |
||
| 541 | ), |
||
| 542 | array( |
||
| 543 | 'name' => 'roles_allowed', |
||
| 544 | 'label' => __( 'Role(s) Allowed', 'pods' ), |
||
| 545 | 'type' => 'pick', |
||
| 546 | 'pick_object' => 'role', |
||
| 547 | 'pick_format_type' => 'multi', |
||
| 548 | 'pick_format_multi' => 'autocomplete', |
||
| 549 | 'pick_ajax' => false, |
||
| 550 | 'default' => '', |
||
| 551 | 'depends-on' => array( |
||
| 552 | 'restrict_role' => true, |
||
| 553 | ), |
||
| 554 | ), |
||
| 555 | array( |
||
| 556 | 'name' => 'restrict_capability', |
||
| 557 | 'label' => __( 'Restrict access by Capability?', 'pods' ), |
||
| 558 | 'help' => array( |
||
| 559 | __( '<h6>Capabilities</h6> Capabilities denote access to specific functionality in WordPress, and are assigned to specific User Roles. Please see the Roles and Capabilities component in Pods for an easy tool to add your own capabilities and roles.', 'pods' ), |
||
| 560 | 'http://codex.wordpress.org/Roles_and_Capabilities', |
||
| 561 | ), |
||
| 562 | 'default' => 0, |
||
| 563 | 'type' => 'boolean', |
||
| 564 | 'dependency' => true, |
||
| 565 | ), |
||
| 566 | array( |
||
| 567 | 'name' => 'capability_allowed', |
||
| 568 | 'label' => __( 'Capability Allowed', 'pods' ), |
||
| 569 | 'type' => 'pick', |
||
| 570 | 'pick_object' => 'capability', |
||
| 571 | 'pick_format_type' => 'multi', |
||
| 572 | 'pick_format_multi' => 'autocomplete', |
||
| 573 | 'pick_ajax' => false, |
||
| 574 | 'default' => '', |
||
| 575 | 'depends-on' => array( |
||
| 576 | 'restrict_capability' => true, |
||
| 577 | ), |
||
| 578 | ), |
||
| 579 | array( |
||
| 580 | 'name' => 'restrict_redirect', |
||
| 581 | 'label' => __( 'Redirect if Restricted?', 'pods' ), |
||
| 582 | 'default' => 0, |
||
| 583 | 'type' => 'boolean', |
||
| 584 | 'dependency' => true, |
||
| 585 | ), |
||
| 586 | array( |
||
| 587 | 'name' => 'restrict_redirect_login', |
||
| 588 | 'label' => __( 'Redirect to WP Login page', 'pods' ), |
||
| 589 | 'default' => 0, |
||
| 590 | 'type' => 'boolean', |
||
| 591 | 'dependency' => true, |
||
| 592 | 'depends-on' => array( |
||
| 593 | 'restrict_redirect' => true, |
||
| 594 | ), |
||
| 595 | ), |
||
| 596 | array( |
||
| 597 | 'name' => 'restrict_redirect_url', |
||
| 598 | 'label' => __( 'Redirect to a Custom URL', 'pods' ), |
||
| 599 | 'default' => '', |
||
| 600 | 'type' => 'text', |
||
| 601 | 'depends-on' => array( |
||
| 602 | 'restrict_redirect' => true, |
||
| 603 | 'restrict_redirect_login' => false, |
||
| 604 | ), |
||
| 605 | ), |
||
| 606 | ); |
||
| 607 | |||
| 608 | pods_group_add( $pod, __( 'Restrict Access', 'pods' ), $fields, 'normal', 'high' ); |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Get the fields |
||
| 613 | * |
||
| 614 | * @param null $_null |
||
| 615 | * @param int $post_ID |
||
| 616 | * @param string $meta_key |
||
| 617 | * @param bool $single |
||
| 618 | * |
||
| 619 | * @return array|bool|int|mixed|null|string|void |
||
| 620 | */ |
||
| 621 | public function get_meta( $_null, $post_ID = null, $meta_key = null, $single = false ) { |
||
| 622 | |||
| 623 | if ( 'code' === $meta_key ) { |
||
| 624 | $post = get_post( $post_ID ); |
||
| 625 | |||
| 626 | if ( is_object( $post ) && $this->object_type == $post->post_type ) { |
||
| 627 | return $post->post_content; |
||
| 628 | } |
||
| 629 | } |
||
| 630 | |||
| 631 | return $_null; |
||
| 632 | } |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Save the fields |
||
| 636 | * |
||
| 637 | * @param $_null |
||
| 638 | * @param int $post_ID |
||
| 639 | * @param string $meta_key |
||
| 640 | * @param null $meta_value |
||
| 641 | * |
||
| 642 | * @return bool|int|null |
||
| 643 | */ |
||
| 644 | public function save_meta( $_null, $post_ID = null, $meta_key = null, $meta_value = null ) { |
||
| 645 | |||
| 646 | if ( 'code' === $meta_key ) { |
||
| 647 | $post = get_post( $post_ID ); |
||
| 648 | |||
| 649 | if ( is_object( $post ) && $this->object_type == $post->post_type ) { |
||
| 650 | $postdata = array( |
||
| 651 | 'ID' => $post_ID, |
||
| 652 | 'post_content' => $meta_value, |
||
| 653 | ); |
||
| 654 | |||
| 655 | remove_filter( current_filter(), array( $this, __FUNCTION__ ) ); |
||
| 656 | |||
| 657 | $revisions = false; |
||
| 658 | |||
| 659 | if ( has_action( 'pre_post_update', 'wp_save_post_revision' ) ) { |
||
| 660 | remove_action( 'pre_post_update', 'wp_save_post_revision' ); |
||
| 661 | |||
| 662 | $revisions = true; |
||
| 663 | } |
||
| 664 | |||
| 665 | wp_update_post( (object) $postdata ); |
||
| 666 | // objects will be automatically sanitized |
||
| 667 | if ( $revisions ) { |
||
| 668 | add_action( 'pre_post_update', 'wp_save_post_revision' ); |
||
| 669 | } |
||
| 670 | |||
| 671 | return true; |
||
| 672 | }//end if |
||
| 673 | }//end if |
||
| 674 | |||
| 675 | return $_null; |
||
| 676 | } |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Flush Pod Page Rewrite cache |
||
| 680 | * |
||
| 681 | * @return array Pod Page Rewrites |
||
| 682 | * |
||
| 683 | * @since 2.3.4 |
||
| 684 | */ |
||
| 685 | public static function flush_rewrites() { |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Check to see if Pod Page exists and return data |
||
| 715 | * |
||
| 716 | * $uri not required, if NULL then returns REQUEST_URI matching Pod Page |
||
| 717 | * |
||
| 718 | * @param string $uri The Pod Page URI to check if exists |
||
| 719 | * |
||
| 720 | * @return array|bool |
||
| 721 | */ |
||
| 722 | public static function exists( $uri = null ) { |
||
| 863 | |||
| 864 | /** |
||
| 865 | * Check if a Pod Page exists |
||
| 866 | */ |
||
| 867 | public function page_check() { |
||
| 868 | |||
| 869 | if ( self::$checked ) { |
||
| 870 | return; |
||
| 871 | } |
||
| 872 | |||
| 873 | global $pods; |
||
| 874 | |||
| 875 | // Fix any global confusion wherever this runs |
||
| 876 | if ( isset( $pods ) && ! isset( $GLOBALS['pods'] ) ) { |
||
| 877 | $GLOBALS['pods'] =& $pods; |
||
| 878 | } elseif ( ! isset( $pods ) && isset( $GLOBALS['pods'] ) ) { |
||
| 879 | $pods =& $GLOBALS['pods']; |
||
| 880 | } |
||
| 881 | |||
| 882 | if ( ! defined( 'PODS_DISABLE_POD_PAGE_CHECK' ) || ! PODS_DISABLE_POD_PAGE_CHECK ) { |
||
| 883 | if ( null === self::$exists ) { |
||
| 884 | self::$exists = pod_page_exists(); |
||
| 885 | } |
||
| 886 | |||
| 887 | if ( false !== self::$exists ) { |
||
| 888 | $pods = apply_filters( 'pods_global', $pods, self::$exists ); |
||
| 889 | |||
| 890 | if ( ! is_wp_error( $pods ) && ( is_object( $pods ) || 404 != $pods ) ) { |
||
| 891 | add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
||
| 892 | add_filter( 'redirect_canonical', '__return_false' ); |
||
| 893 | add_action( 'wp_head', array( $this, 'wp_head' ) ); |
||
| 894 | add_filter( 'wp_title', array( $this, 'wp_title' ), 0, 3 ); |
||
| 895 | add_filter( 'body_class', array( $this, 'body_class' ), 0, 1 ); |
||
| 896 | add_filter( 'status_header', array( $this, 'status_header' ) ); |
||
| 897 | add_action( 'after_setup_theme', array( $this, 'precode' ) ); |
||
| 898 | add_action( 'wp', array( $this, 'silence_404' ), 1 ); |
||
| 899 | |||
| 900 | // Genesis theme integration |
||
| 901 | add_action( 'genesis_loop', 'pods_content', 11 ); |
||
| 902 | } |
||
| 903 | } |
||
| 904 | |||
| 905 | self::$checked = true; |
||
| 906 | }//end if |
||
| 907 | } |
||
| 908 | |||
| 909 | /** |
||
| 910 | * Output Pod Page Content |
||
| 911 | * |
||
| 912 | * @param bool $return Whether to return or not (default is to echo) |
||
| 913 | * |
||
| 914 | * @param bool $pods_page |
||
| 915 | * |
||
| 916 | * @return string |
||
| 917 | */ |
||
| 918 | public static function content( $return = false, $pods_page = false ) { |
||
| 919 | |||
| 920 | if ( empty( $pods_page ) ) { |
||
| 921 | $pods_page = self::$exists; |
||
| 922 | } |
||
| 923 | |||
| 924 | $content = false; |
||
| 925 | |||
| 926 | if ( $pods_page == self::$exists && self::$content_called ) { |
||
| 927 | return $content; |
||
| 928 | } |
||
| 929 | |||
| 930 | if ( ! empty( $pods_page ) ) { |
||
| 931 | /** |
||
| 932 | * @var $pods \Pods |
||
| 933 | */ |
||
| 934 | global $pods; |
||
| 935 | |||
| 936 | // Fix any global confusion wherever this runs |
||
| 937 | if ( isset( $pods ) && ! isset( $GLOBALS['pods'] ) ) { |
||
| 938 | $GLOBALS['pods'] =& $pods; |
||
| 939 | } elseif ( ! isset( $pods ) && isset( $GLOBALS['pods'] ) ) { |
||
| 940 | $pods =& $GLOBALS['pods']; |
||
| 941 | } |
||
| 942 | |||
| 943 | if ( 0 < strlen( trim( $pods_page['code'] ) ) ) { |
||
| 944 | $content = trim( $pods_page['code'] ); |
||
| 945 | } |
||
| 946 | |||
| 947 | ob_start(); |
||
| 948 | |||
| 949 | do_action( 'pods_content_pre', $pods_page, $content ); |
||
| 950 | |||
| 951 | if ( 0 < strlen( $content ) ) { |
||
| 952 | if ( false !== strpos( $content, '<?' ) && ( ! defined( 'PODS_DISABLE_EVAL' ) || ! PODS_DISABLE_EVAL ) ) { |
||
| 953 | pods_deprecated( 'Pod Page PHP code has been deprecated, please use WP Page Templates or hook into the pods_content filter instead of embedding PHP.', '2.1' ); |
||
| 954 | |||
| 955 | eval( "?>$content" ); |
||
| 956 | } elseif ( is_object( $pods ) && ! empty( $pods->id ) ) { |
||
| 957 | echo $pods->do_magic_tags( $content ); |
||
| 958 | } else { |
||
| 959 | echo $content; |
||
| 960 | } |
||
| 961 | } |
||
| 962 | |||
| 963 | do_action( 'pods_content_post', $pods_page, $content ); |
||
| 964 | |||
| 965 | $content = ob_get_clean(); |
||
| 966 | |||
| 967 | if ( $pods_page == self::$exists ) { |
||
| 968 | self::$content_called = true; |
||
| 969 | } |
||
| 970 | }//end if |
||
| 971 | |||
| 972 | $content = apply_filters( 'pods_content', $content, $pods_page ); |
||
| 973 | |||
| 974 | if ( $return ) { |
||
| 975 | return $content; |
||
| 976 | } |
||
| 977 | |||
| 978 | echo $content; |
||
| 979 | } |
||
| 980 | |||
| 981 | /** |
||
| 982 | * Run any precode for current Pod Page |
||
| 983 | */ |
||
| 984 | public function precode() { |
||
| 985 | |||
| 986 | global $pods; |
||
| 987 | |||
| 988 | // Fix any global confusion wherever this runs |
||
| 989 | if ( isset( $pods ) && ! isset( $GLOBALS['pods'] ) ) { |
||
| 990 | $GLOBALS['pods'] =& $pods; |
||
| 991 | } elseif ( ! isset( $pods ) && isset( $GLOBALS['pods'] ) ) { |
||
| 992 | $pods =& $GLOBALS['pods']; |
||
| 993 | } |
||
| 994 | |||
| 995 | if ( false !== self::$exists ) { |
||
| 996 | $permission = pods_permission( self::$exists['options'] ); |
||
| 997 | |||
| 998 | $permission = (boolean) apply_filters( 'pods_pages_permission', $permission, self::$exists ); |
||
| 999 | |||
| 1000 | if ( $permission ) { |
||
| 1001 | $content = false; |
||
| 1002 | |||
| 1003 | if ( ! is_object( $pods ) && 404 != $pods && 0 < strlen( pods_var( 'pod', self::$exists['options'] ) ) ) { |
||
| 1004 | $slug = pods_var_raw( 'pod_slug', self::$exists['options'], null, null, true ); |
||
| 1005 | |||
| 1006 | // Handle special magic tags |
||
| 1007 | if ( 0 < strlen( $slug ) ) { |
||
| 1008 | $slug = pods_evaluate_tags( $slug, true ); |
||
| 1009 | } |
||
| 1010 | |||
| 1011 | $pods = pods( pods_var( 'pod', self::$exists['options'] ), $slug ); |
||
| 1012 | |||
| 1013 | // Auto 404 handling if item doesn't exist |
||
| 1014 | if ( 0 < strlen( $slug ) && ! $pods->exists() && apply_filters( 'pods_pages_auto_404', true, $slug, $pods, self::$exists ) ) { |
||
| 1015 | $pods = 404; |
||
| 1016 | } |
||
| 1017 | } |
||
| 1018 | |||
| 1019 | if ( 0 < strlen( trim( self::$exists['precode'] ) ) ) { |
||
| 1020 | $content = self::$exists['precode']; |
||
| 1021 | } |
||
| 1022 | |||
| 1023 | if ( false !== $content && ( ! defined( 'PODS_DISABLE_EVAL' ) || ! PODS_DISABLE_EVAL ) ) { |
||
| 1024 | pods_deprecated( 'Pod Page Precode has been deprecated, please use WP Page Templates or hook into the pods_content filter instead of embedding PHP.', '2.1' ); |
||
| 1025 | |||
| 1026 | eval( "?>$content" ); |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | do_action( 'pods_page_precode', self::$exists, $pods, $content ); |
||
| 1030 | } elseif ( self::$exists['options']['restrict_redirect'] ) { |
||
| 1031 | $redirect_url = ''; |
||
| 1032 | |||
| 1033 | if ( self::$exists['options']['restrict_redirect_login'] ) { |
||
| 1034 | $redirect_url = wp_login_url( pods_current_url() ); |
||
| 1035 | } elseif ( ! empty( self::$exists['options']['restrict_redirect_url'] ) ) { |
||
| 1036 | $redirect_url = self::$exists['options']['restrict_redirect_url']; |
||
| 1037 | } |
||
| 1038 | |||
| 1039 | if ( ! empty( $redirect_url ) ) { |
||
| 1040 | wp_redirect( $redirect_url ); |
||
| 1041 | die(); |
||
| 1042 | } |
||
| 1043 | }//end if |
||
| 1044 | |||
| 1045 | if ( ! $permission || ( ! is_object( $pods ) && ( 404 == $pods || is_wp_error( $pods ) ) ) ) { |
||
| 1046 | remove_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
||
| 1047 | remove_action( 'wp_head', array( $this, 'wp_head' ) ); |
||
| 1048 | remove_filter( 'redirect_canonical', '__return_false' ); |
||
| 1049 | remove_filter( 'wp_title', array( $this, 'wp_title' ) ); |
||
| 1050 | remove_filter( 'body_class', array( $this, 'body_class' ) ); |
||
| 1051 | remove_filter( 'status_header', array( $this, 'status_header' ) ); |
||
| 1052 | remove_action( 'wp', array( $this, 'silence_404' ), 1 ); |
||
| 1053 | } |
||
| 1054 | }//end if |
||
| 1055 | } |
||
| 1056 | |||
| 1057 | /** |
||
| 1058 | * |
||
| 1059 | */ |
||
| 1060 | public function wp_head() { |
||
| 1061 | |||
| 1062 | global $pods; |
||
| 1063 | |||
| 1064 | do_action( 'pods_wp_head' ); |
||
| 1065 | |||
| 1066 | if ( ! defined( 'PODS_DISABLE_VERSION_OUTPUT' ) || ! PODS_DISABLE_VERSION_OUTPUT ) { |
||
| 1067 | ?> |
||
| 1068 | <!-- Pods Framework <?php echo esc_html( PODS_VERSION ); ?> --> |
||
| 1069 | <?php |
||
| 1070 | } |
||
| 1071 | if ( ( ! defined( 'PODS_DISABLE_META' ) || ! PODS_DISABLE_META ) && is_object( $pods ) && ! is_wp_error( $pods ) ) { |
||
| 1072 | |||
| 1073 | if ( isset( $pods->meta ) && is_array( $pods->meta ) && ! empty( $pods->meta ) ) { |
||
| 1074 | foreach ( $pods->meta as $name => $content ) { |
||
| 1075 | if ( 'title' === $name ) { |
||
| 1076 | continue; |
||
| 1077 | } |
||
| 1078 | ?> |
||
| 1079 | <meta name="<?php echo esc_attr( $name ); ?>" content="<?php echo esc_attr( $content ); ?>" /> |
||
| 1080 | <?php |
||
| 1081 | } |
||
| 1082 | } |
||
| 1083 | |||
| 1084 | if ( isset( $pods->meta_properties ) && is_array( $pods->meta_properties ) && ! empty( $pods->meta_properties ) ) { |
||
| 1085 | foreach ( $pods->meta_properties as $property => $content ) { |
||
| 1086 | ?> |
||
| 1087 | <meta property="<?php echo esc_attr( $property ); ?>" content="<?php echo esc_attr( $content ); ?>" /> |
||
| 1088 | <?php |
||
| 1089 | } |
||
| 1090 | } |
||
| 1091 | |||
| 1092 | if ( isset( $pods->meta_extra ) && 0 < strlen( $pods->meta_extra ) ) { |
||
| 1093 | echo $pods->meta_extra; |
||
| 1094 | } |
||
| 1095 | }//end if |
||
| 1096 | } |
||
| 1097 | |||
| 1098 | /** |
||
| 1099 | * @param $title |
||
| 1100 | * @param $sep |
||
| 1101 | * @param $seplocation |
||
| 1102 | * |
||
| 1103 | * @return mixed|void |
||
| 1104 | */ |
||
| 1105 | public function wp_title( $title, $sep, $seplocation ) { |
||
| 1136 | |||
| 1137 | /** |
||
| 1138 | * @param $classes |
||
| 1139 | * |
||
| 1140 | * @return mixed|void |
||
| 1141 | */ |
||
| 1142 | public function body_class( $classes ) { |
||
| 1175 | |||
| 1176 | /** |
||
| 1177 | * @return string |
||
| 1178 | */ |
||
| 1179 | public function status_header() { |
||
| 1183 | |||
| 1184 | /** |
||
| 1185 | * |
||
| 1186 | */ |
||
| 1187 | public function silence_404() { |
||
| 1194 | |||
| 1195 | /** |
||
| 1196 | * |
||
| 1197 | */ |
||
| 1198 | public function template_redirect() { |
||
| 1271 | } |
||
| 1272 | |||
| 1273 | /** |
||
| 1274 | * Find out if the current page is a Pod Page |
||
| 1275 | * |
||
| 1276 | * @param string $uri The Pod Page URI to check if currently on |
||
| 1277 | * |
||
| 1278 | * @return bool |
||
| 1279 | * @since 1.7.5 |
||
| 1280 | */ |
||
| 1281 | function is_pod_page( $uri = null ) { |
||
| 1282 | |||
| 1283 | if ( false !== Pods_Pages::$exists && ( null === $uri || $uri == Pods_Pages::$exists['uri'] || $uri == Pods_Pages::$exists['id'] ) ) { |
||
| 1284 | return true; |
||
| 1386 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.