PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/components/Pages.php

https://github.com/ElmsPark/pods
PHP | 898 lines | 528 code | 166 blank | 204 comment | 142 complexity | ac75de264f5bb2c801c2b0cffee3d31e MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * Name: Pages
  4. *
  5. * Description: Create advanced URL structures using wildcards, they can exist on-top of any existing WordPress URL rewrites or be entirely custom. Add a path and select the WP Template to use, the rest is up to you!
  6. *
  7. * Version: 2.0
  8. *
  9. * Category: Advanced
  10. *
  11. * Menu Page: edit.php?post_type=_pods_page
  12. * Menu Add Page: post-new.php?post_type=_pods_page
  13. *
  14. * @package Pods\Components
  15. * @subpackage Pages
  16. */
  17. class Pods_Pages extends PodsComponent {
  18. /**
  19. * Current Pod Page
  20. *
  21. * @var array
  22. *
  23. * @since 2.0.0
  24. */
  25. static $exists = null;
  26. /**
  27. * Object type
  28. *
  29. * @var string
  30. *
  31. * @since 2.0.0
  32. */
  33. private $object_type = '_pods_page';
  34. /**
  35. * Whether the page has been checked already
  36. *
  37. * @var bool
  38. *
  39. * @since 2.1.0
  40. */
  41. static $checked = false;
  42. /**
  43. * Do things like register/enqueue scripts and stylesheets
  44. *
  45. * @since 2.0.0
  46. */
  47. public function __construct () {
  48. $args = array(
  49. 'label' => 'Pod Pages',
  50. 'labels' => array( 'singular_name' => 'Pod Page' ),
  51. 'public' => false,
  52. 'can_export' => false,
  53. 'show_ui' => true,
  54. 'show_in_menu' => false,
  55. 'query_var' => false,
  56. 'rewrite' => false,
  57. 'has_archive' => false,
  58. 'hierarchical' => false,
  59. 'supports' => array( 'title', 'author', 'revisions' ),
  60. 'menu_icon' => PODS_URL . 'ui/images/icon16.png'
  61. );
  62. if ( !is_super_admin() )
  63. $args[ 'capability_type' ] = 'pods_page';
  64. $args = PodsInit::object_label_fix( $args, 'post_type' );
  65. register_post_type( $this->object_type, apply_filters( 'pods_internal_register_post_type_object_page', $args ) );
  66. if ( !is_admin() )
  67. add_action( 'load_textdomain', array( $this, 'page_check' ), 12 );
  68. else {
  69. add_filter( 'post_updated_messages', array( $this, 'setup_updated_messages' ), 10, 1 );
  70. add_action( 'dbx_post_advanced', array( $this, 'edit_page_form' ), 10 );
  71. add_action( 'pods_meta_groups', array( $this, 'add_meta_boxes' ) );
  72. add_filter( 'get_post_metadata', array( $this, 'get_meta' ), 10, 4 );
  73. add_filter( 'update_post_metadata', array( $this, 'save_meta' ), 10, 4 );
  74. add_action( 'pods_meta_save_pre_post__pods_page', array( $this, 'fix_filters' ), 10, 5 );
  75. add_action( 'post_updated', array( $this, 'clear_cache' ), 10, 3 );
  76. add_action( 'delete_post', array( $this, 'clear_cache' ), 10, 1 );
  77. add_filter( 'post_row_actions', array( $this, 'remove_row_actions' ), 10, 2 );
  78. add_filter( 'bulk_actions-edit-' . $this->object_type, array( $this, 'remove_bulk_actions' ) );
  79. add_filter( 'builder_layout_filter_non_layout_post_types', array( $this, 'disable_builder_layout' ) );
  80. }
  81. }
  82. public function disable_builder_layout ( $post_types ) {
  83. $post_types[] = $this->object_type;
  84. return $post_types;
  85. }
  86. /**
  87. * Update Post Type messages
  88. *
  89. * @param array $messages
  90. *
  91. * @return array
  92. * @since 2.0.2
  93. */
  94. public function setup_updated_messages ( $messages ) {
  95. global $post, $post_ID;
  96. $post_type = get_post_type_object( $this->object_type );
  97. $labels = $post_type->labels;
  98. $messages[ $post_type->name ] = array(
  99. 1 => sprintf( __( '%s updated. <a href="%s">%s</a>', 'pods' ), $labels->singular_name, esc_url( get_permalink( $post_ID ) ), $labels->view_item ),
  100. 2 => __( 'Custom field updated.', 'pods' ),
  101. 3 => __( 'Custom field deleted.', 'pods' ),
  102. 4 => sprintf( __( '%s updated.', 'pods' ), $labels->singular_name ),
  103. /* translators: %s: date and time of the revision */
  104. 5 => isset( $_GET[ 'revision' ] ) ? sprintf( __( '%s restored to revision from %s', 'pods' ), $labels->singular_name, wp_post_revision_title( (int) $_GET[ 'revision' ], false ) ) : false,
  105. 6 => sprintf( __( '%s published. <a href="%s">%s</a>', 'pods' ), $labels->singular_name, esc_url( get_permalink( $post_ID ) ), $labels->view_item ),
  106. 7 => sprintf( __( '%s saved.', 'pods' ), $labels->singular_name ),
  107. 8 => sprintf( __( '%s submitted. <a target="_blank" href="%s">Preview %s</a>', 'pods' ),
  108. $labels->singular_name,
  109. esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ),
  110. $labels->singular_name
  111. ),
  112. 9 => sprintf( __( '%s scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview %s</a>', 'pods' ),
  113. $labels->singular_name,
  114. // translators: Publish box date format, see http://php.net/date
  115. date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ),
  116. esc_url( get_permalink( $post_ID ) ),
  117. $labels->singular_name
  118. ),
  119. 10 => sprintf( __( '%s draft updated. <a target="_blank" href="%s">Preview %s</a>', 'pods' ), $labels->singular_name, esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ), $labels->singular_name )
  120. );
  121. if ( false === (boolean) $post_type->public ) {
  122. $messages[ $post_type->name ][ 1 ] = sprintf( __( '%s updated.', 'pods' ), $labels->singular_name );
  123. $messages[ $post_type->name ][ 6 ] = sprintf( __( '%s published.', 'pods' ), $labels->singular_name );
  124. $messages[ $post_type->name ][ 8 ] = sprintf( __( '%s submitted.', 'pods' ), $labels->singular_name );
  125. $messages[ $post_type->name ][ 9 ] = sprintf( __( '%s scheduled for: <strong>%1$s</strong>.', 'pods' ),
  126. $labels->singular_name,
  127. // translators: Publish box date format, see http://php.net/date
  128. date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) )
  129. );
  130. $messages[ $post_type->name ][ 10 ] = sprintf( __( '%s draft updated.', 'pods' ), $labels->singular_name );
  131. }
  132. return $messages;
  133. }
  134. /**
  135. * Enqueue styles
  136. *
  137. * @since 2.0.0
  138. */
  139. public function admin_assets () {
  140. wp_enqueue_style( 'pods-admin' );
  141. }
  142. /**
  143. * Fix filters, specifically removing balanceTags
  144. *
  145. * @since 2.0.1
  146. */
  147. public function fix_filters( $data, $pod = null, $id = null, $groups = null, $post = null ) {
  148. remove_filter( 'content_save_pre', 'balanceTags', 50 );
  149. }
  150. /**
  151. * Remove unused row actions
  152. *
  153. * @since 2.0.5
  154. */
  155. public function remove_row_actions ( $actions, $post ) {
  156. global $current_screen;
  157. if ( $this->object_type != $current_screen->post_type )
  158. return $actions;
  159. if ( isset( $actions[ 'view' ] ) )
  160. unset( $actions[ 'view' ] );
  161. if ( isset( $actions[ 'inline hide-if-no-js' ] ) )
  162. unset( $actions[ 'inline hide-if-no-js' ] );
  163. // W3 Total Cache
  164. if ( isset( $actions[ 'pgcache_purge' ] ) )
  165. unset( $actions[ 'pgcache_purge' ] );
  166. return $actions;
  167. }
  168. /**
  169. * Remove unused bulk actions
  170. *
  171. * @since 2.0.5
  172. */
  173. public function remove_bulk_actions ( $actions ) {
  174. if ( isset( $actions[ 'edit' ] ) )
  175. unset( $actions[ 'edit' ] );
  176. return $actions;
  177. }
  178. /**
  179. * Clear cache on save
  180. *
  181. * @since 2.0.0
  182. */
  183. public function clear_cache ( $data, $pod = null, $id = null, $groups = null, $post = null ) {
  184. if ( !is_array( $data ) && 0 < $data ) {
  185. $post = $data;
  186. $post = get_post( $post );
  187. if ( is_object( $id ) ) {
  188. $old_post = $id;
  189. pods_transient_clear( 'pods_object_page_' . $old_post->post_title );
  190. pods_cache_clear( $old_post->post_title, 'pods_object_page_wildcard' );
  191. }
  192. }
  193. if ( $this->object_type != $post->post_type )
  194. return;
  195. pods_transient_clear( 'pods_object_page_' . $post->post_title );
  196. pods_cache_clear( $post->post_title, 'pods_object_page_wildcard' );
  197. }
  198. /**
  199. * Change post title placeholder text
  200. *
  201. * @since 2.0.0
  202. */
  203. public function set_title_text ( $text, $post ) {
  204. return __( 'Enter URL here', 'pods' );
  205. }
  206. /**
  207. * Edit page form
  208. *
  209. * @since 2.0.0
  210. */
  211. public function edit_page_form () {
  212. global $post_type;
  213. if ( $this->object_type != $post_type )
  214. return;
  215. add_filter( 'enter_title_here', array( $this, 'set_title_text' ), 10, 2 );
  216. }
  217. /**
  218. * Add meta boxes to the page
  219. *
  220. * @since 2.0.0
  221. */
  222. public function add_meta_boxes () {
  223. $pod = array(
  224. 'name' => $this->object_type,
  225. 'type' => 'post_type'
  226. );
  227. if ( isset( PodsMeta::$post_types[ $pod[ 'name' ] ] ) )
  228. return;
  229. if ( !function_exists( 'get_page_templates' ) )
  230. include_once ABSPATH . 'wp-admin/includes/theme.php';
  231. $page_templates = apply_filters( 'pods_page_templates', get_page_templates() );
  232. $page_templates[ __( '-- Page Template --', 'pods' ) ] = '';
  233. if ( !in_array( 'pods.php', $page_templates ) && locate_template( array( 'pods.php', false ) ) )
  234. $page_templates[ 'Pods (Pods Default)' ] = 'pods.php';
  235. if ( !in_array( 'page.php', $page_templates ) && locate_template( array( 'page.php', false ) ) )
  236. $page_templates[ 'Page (WP Default)' ] = 'page.php';
  237. if ( !in_array( 'index.php', $page_templates ) && locate_template( array( 'index.php', false ) ) )
  238. $page_templates[ 'Index (WP Fallback)' ] = 'index.php';
  239. ksort( $page_templates );
  240. $page_templates = array_flip( $page_templates );
  241. $fields = array(
  242. array(
  243. 'name' => 'page_title',
  244. 'label' => __( 'Page Title', 'pods' ),
  245. 'type' => 'text'
  246. ),
  247. array(
  248. 'name' => 'code',
  249. 'label' => __( 'Page Code', 'pods' ),
  250. 'type' => 'code',
  251. 'attributes' => array(
  252. 'id' => 'content'
  253. ),
  254. 'label_options' => array(
  255. 'attributes' => array(
  256. 'for' => 'content'
  257. )
  258. )
  259. ),
  260. array(
  261. 'name' => 'precode',
  262. 'label' => __( 'Page Precode', 'pods' ),
  263. 'type' => 'code',
  264. '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' )
  265. ),
  266. array(
  267. 'name' => 'page_template',
  268. 'label' => __( 'Page Template', 'pods' ),
  269. 'type' => 'pick',
  270. 'data' => $page_templates
  271. )
  272. );
  273. pods_group_add( $pod, __( 'Page', 'pods' ), $fields, 'normal', 'high' );
  274. $fields = array(
  275. array(
  276. 'name' => 'admin_only',
  277. 'label' => __( 'Show to Admins Only?', 'pods' ),
  278. 'default' => 0,
  279. 'type' => 'boolean',
  280. 'dependency' => true
  281. ),
  282. array(
  283. 'name' => 'restrict_capability',
  284. 'label' => __( 'Restrict access by Capability?', 'pods' ),
  285. 'default' => 0,
  286. 'type' => 'boolean',
  287. 'dependency' => true
  288. ),
  289. array(
  290. 'name' => 'capability_allowed',
  291. 'label' => __( 'Capability Allowed', 'pods' ),
  292. 'help' => __( 'Comma-separated list of cababilities, for example add_podname_item, please see the Roles and Capabilities component for the complete list and a way to add your own.', 'pods' ),
  293. 'type' => 'text',
  294. 'default' => '',
  295. 'depends-on' => array( 'restrict_capability' => true )
  296. )
  297. );
  298. pods_group_add( $pod, __( 'Restrict Access', 'pods' ), $fields, 'normal', 'high' );
  299. }
  300. /**
  301. * Get the fields
  302. *
  303. * @param null $_null
  304. * @param int $post_ID
  305. * @param string $meta_key
  306. * @param bool $single
  307. *
  308. * @return array|bool|int|mixed|null|string|void
  309. */
  310. public function get_meta ( $_null, $post_ID = null, $meta_key = null, $single = false ) {
  311. if ( 'code' == $meta_key ) {
  312. $post = get_post( $post_ID );
  313. if ( is_object( $post ) && $this->object_type == $post->post_type )
  314. return $post->post_content;
  315. }
  316. return $_null;
  317. }
  318. /**
  319. * Save the fields
  320. *
  321. * @param $_null
  322. * @param int $post_ID
  323. * @param string $meta_key
  324. * @param null $meta_value
  325. *
  326. * @return bool|int|null
  327. */
  328. public function save_meta ( $_null, $post_ID = null, $meta_key = null, $meta_value = null ) {
  329. if ( 'code' == $meta_key ) {
  330. $post = get_post( $post_ID );
  331. if ( is_object( $post ) && $this->object_type == $post->post_type ) {
  332. $postdata = array(
  333. 'ID' => $post_ID,
  334. 'post_content' => pods_sanitize( $meta_value )
  335. );
  336. remove_filter( current_filter(), array( $this, __FUNCTION__ ), 10 );
  337. $revisions = false;
  338. if ( has_action( 'pre_post_update', 'wp_save_post_revision' ) ) {
  339. remove_action( 'pre_post_update', 'wp_save_post_revision' );
  340. $revisions = true;
  341. }
  342. wp_update_post( $postdata );
  343. if ( $revisions )
  344. add_action( 'pre_post_update', 'wp_save_post_revision' );
  345. return true;
  346. }
  347. }
  348. return $_null;
  349. }
  350. /**
  351. * Check to see if Pod Page exists and return data
  352. *
  353. * $uri not required, if NULL then returns REQUEST_URI matching Pod Page
  354. *
  355. * @param string $uri The Pod Page URI to check if exists
  356. *
  357. * @return array|bool
  358. */
  359. public static function exists ( $uri = null ) {
  360. if ( null === $uri ) {
  361. $uri = parse_url( get_current_url() );
  362. $uri = $uri[ 'path' ];
  363. $home = parse_url( get_bloginfo( 'url' ) );
  364. if ( !empty( $home ) && isset( $home[ 'path' ] ) && '/' != $home[ 'path' ] )
  365. $uri = substr( $uri, strlen( $home[ 'path' ] ) );
  366. }
  367. $uri = trim( $uri, '/' );
  368. $uri_depth = count( array_filter( explode( '/', $uri ) ) ) - 1;
  369. if ( false !== strpos( $uri, 'wp-admin' ) || false !== strpos( $uri, 'wp-includes' ) )
  370. return false;
  371. $object = pods_transient_get( 'pods_object_page_' . $uri );
  372. if ( false !== $object )
  373. return $object;
  374. // See if the custom template exists
  375. $sql = "
  376. SELECT *
  377. FROM `@wp_posts`
  378. WHERE
  379. `post_type` = '_pods_page'
  380. AND `post_status` = 'publish'
  381. AND `post_title` = %s
  382. LIMIT 1
  383. ";
  384. $sql = array( $sql, array( $uri ) );
  385. $result = pods_query( $sql );
  386. $wildcard = false;
  387. if ( empty( $result ) ) {
  388. $object = pods_cache_get( $uri, 'pods_object_page_wildcard' );
  389. if ( false !== $object )
  390. return $object;
  391. // Find any wildcards
  392. $sql = "
  393. SELECT *
  394. FROM `@wp_posts`
  395. WHERE
  396. `post_type` = '_pods_page'
  397. AND `post_status` = 'publish'
  398. AND %s LIKE REPLACE(`post_title`, '*', '%%')
  399. AND (LENGTH(`post_title`) - LENGTH(REPLACE(`post_title`, '/', ''))) = %d
  400. ORDER BY LENGTH(`post_title`) DESC, `post_title` DESC
  401. LIMIT 1
  402. ";
  403. $sql = array( $sql, array( $uri, $uri_depth ) );
  404. $result = pods_query( $sql );
  405. $wildcard = true;
  406. }
  407. if ( !empty( $result ) ) {
  408. $_object = get_object_vars( $result[ 0 ] );
  409. $object = array(
  410. 'id' => $_object[ 'ID' ],
  411. 'uri' => $_object[ 'post_title' ],
  412. 'code' => $_object[ 'post_content' ],
  413. 'phpcode' => $_object[ 'post_content' ], // phpcode is deprecated
  414. 'precode' => get_post_meta( $_object[ 'ID' ], 'precode', true ),
  415. 'page_template' => get_post_meta( $_object[ 'ID' ], 'page_template', true ),
  416. 'title' => get_post_meta( $_object[ 'ID' ], 'page_title', true ),
  417. 'options' => array(
  418. 'admin_only' => (boolean) get_post_meta( $_object[ 'ID' ], 'admin_only', true ),
  419. 'restrict_capability' => (boolean) get_post_meta( $_object[ 'ID' ], 'restrict_capability', true ),
  420. 'capability_allowed' => get_post_meta( $_object[ 'ID' ], 'capability_allowed', true )
  421. )
  422. );
  423. if ( $wildcard )
  424. pods_cache_set( $uri, $object, 'pods_object_page_wildcard', 3600 );
  425. else
  426. pods_transient_set( 'pods_object_page_' . $uri, $object );
  427. return $object;
  428. }
  429. return false;
  430. }
  431. /**
  432. * Check if a Pod Page exists
  433. */
  434. public function page_check () {
  435. if ( self::$checked )
  436. return;
  437. global $pods;
  438. // Fix any global confusion wherever this runs
  439. if ( isset( $pods ) && !isset( $GLOBALS[ 'pods' ] ) )
  440. $GLOBALS[ 'pods' ] =& $pods;
  441. elseif ( !isset( $pods ) && isset( $GLOBALS[ 'pods' ] ) )
  442. $pods =& $GLOBALS[ 'pods' ];
  443. if ( !defined( 'PODS_DISABLE_POD_PAGE_CHECK' ) || !PODS_DISABLE_POD_PAGE_CHECK ) {
  444. if ( null === self::$exists )
  445. self::$exists = pod_page_exists();
  446. if ( false !== self::$exists ) {
  447. $pods = apply_filters( 'pods_global', $pods, self::$exists );
  448. if ( !is_wp_error( $pods ) && ( is_object( $pods ) || 404 != $pods ) ) {
  449. add_action( 'template_redirect', array( $this, 'template_redirect' ) );
  450. add_filter( 'redirect_canonical', '__return_false' );
  451. add_action( 'wp_head', array( $this, 'wp_head' ) );
  452. add_filter( 'wp_title', array( $this, 'wp_title' ), 0, 3 );
  453. add_filter( 'body_class', array( $this, 'body_class' ), 0, 1 );
  454. add_filter( 'status_header', array( $this, 'status_header' ) );
  455. add_action( 'after_setup_theme', array( $this, 'precode' ) );
  456. add_action( 'wp', array( $this, 'silence_404' ) );
  457. }
  458. }
  459. self::$checked = true;
  460. }
  461. }
  462. /**
  463. * Output Pod Page Content
  464. *
  465. * @param bool $return Whether to return or not (default is to echo)
  466. *
  467. * @return string
  468. */
  469. public static function content ( $return = false ) {
  470. global $pods;
  471. // Fix any global confusion wherever this runs
  472. if ( isset( $pods ) && !isset( $GLOBALS[ 'pods' ] ) )
  473. $GLOBALS[ 'pods' ] =& $pods;
  474. elseif ( !isset( $pods ) && isset( $GLOBALS[ 'pods' ] ) )
  475. $pods =& $GLOBALS[ 'pods' ];
  476. $content = false;
  477. if ( false !== self::$exists ) {
  478. if ( 0 < strlen( trim( self::$exists[ 'code' ] ) ) )
  479. $content = self::$exists[ 'code' ];
  480. ob_start();
  481. do_action( 'pods_content_pre', self::$exists, $content );
  482. if ( false !== $content ) {
  483. if ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) {
  484. pods_deprecated( 'Use WP Page Templates or hook into the pods_content filter instead of using Pod Page PHP code', '2.1.0' );
  485. eval( "?>$content" );
  486. }
  487. else
  488. echo $content;
  489. }
  490. do_action( 'pods_content_post', self::$exists, $content );
  491. $content = ob_get_clean();
  492. }
  493. $content = apply_filters( 'pods_content', $content, self::$exists );
  494. if ( $return )
  495. return $content;
  496. echo $content;
  497. }
  498. /**
  499. * Run any precode for current Pod Page
  500. */
  501. public function precode () {
  502. global $pods;
  503. // Fix any global confusion wherever this runs
  504. if ( isset( $pods ) && !isset( $GLOBALS[ 'pods' ] ) )
  505. $GLOBALS[ 'pods' ] =& $pods;
  506. elseif ( !isset( $pods ) && isset( $GLOBALS[ 'pods' ] ) )
  507. $pods =& $GLOBALS[ 'pods' ];
  508. if ( false !== self::$exists ) {
  509. $permission = pods_permission( self::$exists[ 'options' ] );
  510. $permission = (boolean) apply_filters( 'pods_pages_permission', $permission, self::$exists );
  511. if ( $permission ) {
  512. $content = false;
  513. if ( 0 < strlen( trim( self::$exists[ 'precode' ] ) ) )
  514. $content = self::$exists[ 'precode' ];
  515. if ( false !== $content && ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) ) {
  516. pods_deprecated( 'Use WP Page Templates or hook into the pods_page_precode action instead of using Pod Page Precode', '2.1.0' );
  517. eval( "?>$content" );
  518. }
  519. do_action( 'pods_page_precode', self::$exists, $pods, $content );
  520. }
  521. if ( !$permission || ( !is_object( $pods ) && ( 404 == $pods || is_wp_error( $pods ) ) ) ) {
  522. remove_action( 'template_redirect', array( $this, 'template_redirect' ) );
  523. remove_action( 'wp_head', array( $this, 'wp_head' ) );
  524. remove_filter( 'redirect_canonical', '__return_false' );
  525. remove_filter( 'wp_title', array( $this, 'wp_title' ) );
  526. remove_filter( 'body_class', array( $this, 'body_class' ) );
  527. remove_filter( 'status_header', array( $this, 'status_header' ) );
  528. remove_action( 'wp', array( $this, 'silence_404' ) );
  529. }
  530. }
  531. }
  532. /**
  533. *
  534. */
  535. public function wp_head () {
  536. global $pods;
  537. do_action( 'pods_wp_head' );
  538. if ( !defined( 'PODS_DISABLE_VERSION_OUTPUT' ) || !PODS_DISABLE_VERSION_OUTPUT ) {
  539. ?>
  540. <!-- Pods Framework <?php echo PODS_VERSION; ?> -->
  541. <?php
  542. }
  543. if ( ( !defined( 'PODS_DISABLE_META' ) || !PODS_DISABLE_META ) && is_object( $pods ) && !is_wp_error( $pods ) ) {
  544. if ( isset( $pods->meta ) && is_array( $pods->meta ) && !empty( $pods->meta ) ) {
  545. foreach ( $pods->meta as $name => $content ) {
  546. if ( 'title' == $name )
  547. continue;
  548. ?>
  549. <meta name="<?php echo esc_attr( $name ); ?>" content="<?php echo esc_attr( $content ); ?>" />
  550. <?php
  551. }
  552. }
  553. if ( isset( $pods->meta_properties ) && is_array( $pods->meta_properties ) && !empty( $pods->meta_properties ) ) {
  554. foreach ( $pods->meta_properties as $property => $content ) {
  555. ?>
  556. <meta property="<?php echo esc_attr( $property ); ?>" content="<?php echo esc_attr( $content ); ?>" />
  557. <?php
  558. }
  559. }
  560. if ( isset( $pods->meta_extra ) && 0 < strlen( $pods->meta_extra ) )
  561. echo $pods->meta_extra;
  562. }
  563. }
  564. /**
  565. * @param $title
  566. * @param $sep
  567. * @param $seplocation
  568. *
  569. * @return mixed|void
  570. */
  571. public function wp_title ( $title, $sep, $seplocation ) {
  572. global $pods;
  573. $page_title = trim( self::$exists[ 'title' ] );
  574. if ( 0 < strlen( $page_title ) ) {
  575. if ( is_object( $pods ) && !is_wp_error( $pods ) )
  576. $page_title = $pods->do_magic_tags( $page_title );
  577. $title = ( 'right' == $seplocation ) ? "{$page_title} {$sep} " : " {$sep} {$page_title}";
  578. }
  579. elseif ( strlen( trim( $title ) ) < 1 ) {
  580. $uri = explode( '?', $_SERVER[ 'REQUEST_URI' ] );
  581. $uri = preg_replace( '@^([/]?)(.*?)([/]?)$@', '$2', $uri[ 0 ] );
  582. $uri = preg_replace( '@(-|_)@', ' ', $uri );
  583. $uri = explode( '/', $uri );
  584. $title = '';
  585. foreach ( $uri as $key => $page_title ) {
  586. $title .= ( 'right' == $seplocation ) ? ucwords( $page_title ) . " {$sep} " : " {$sep} " . ucwords( $page_title );
  587. }
  588. }
  589. if ( ( !defined( 'PODS_DISABLE_META' ) || !PODS_DISABLE_META ) && is_object( $pods ) && !is_wp_error( $pods ) && isset( $pods->meta ) && is_array( $pods->meta ) && isset( $pods->meta[ 'title' ] ) )
  590. $title = $pods->meta[ 'title' ];
  591. return apply_filters( 'pods_title', $title, $sep, $seplocation, self::$exists );
  592. }
  593. /**
  594. * @param $classes
  595. *
  596. * @return mixed|void
  597. */
  598. public function body_class ( $classes ) {
  599. global $pods;
  600. if ( defined( 'PODS_DISABLE_BODY_CLASSES' ) && PODS_DISABLE_BODY_CLASSES )
  601. return $classes;
  602. $classes[] = 'pods';
  603. $uri = explode( '?', self::$exists[ 'uri' ] );
  604. $uri = explode( '#', $uri[ 0 ] );
  605. $class = str_replace( array( '*', '/' ), array( '_w_', '-' ), $uri[ 0 ] );
  606. $class = sanitize_title( $class );
  607. $class = str_replace( array( '_', '--', '--' ), '-', $class );
  608. $class = trim( $class, '-' );
  609. $classes[] = 'pod-page-' . $class;
  610. if ( is_object( $pods ) && !is_wp_error( $pods ) ) {
  611. $class = sanitize_title( $pods->pod );
  612. $class = str_replace( array( '_', '--', '--' ), '-', $class );
  613. $class = trim( $class, '-' );
  614. $classes[] = 'pod-' . $class;
  615. }
  616. if ( is_object( $pods ) && !is_wp_error( $pods ) && isset( $pods->body_classes ) )
  617. $classes[] = $pods->body_classes;
  618. return apply_filters( 'pods_body_class', $classes, $uri );
  619. }
  620. /**
  621. * @return string
  622. */
  623. public function status_header () {
  624. return $_SERVER[ 'SERVER_PROTOCOL' ] . ' 200 OK';
  625. }
  626. /**
  627. *
  628. */
  629. public function silence_404 () {
  630. global $wp_query;
  631. $wp_query->query_vars[ 'error' ] = '';
  632. $wp_query->is_404 = false;
  633. }
  634. /**
  635. *
  636. */
  637. public function template_redirect () {
  638. global $pods;
  639. if ( false !== self::$exists ) {
  640. /*
  641. * Create pods.php in your theme directory, and
  642. * style it to suit your needs. Some helpful functions:
  643. *
  644. * get_header()
  645. * pods_content()
  646. * get_sidebar()
  647. * get_footer()
  648. */
  649. $template = self::$exists[ 'page_template' ];
  650. $template = apply_filters( 'pods_page_template', $template, self::$exists );
  651. $render_function = apply_filters( 'pods_template_redirect', null, $template, self::$exists );
  652. do_action( 'pods_page', $template, self::$exists );
  653. if ( null !== $render_function && is_callable( $render_function ) )
  654. call_user_func( $render_function, $template, self::$exists );
  655. elseif ( ( !defined( 'PODS_DISABLE_DYNAMIC_TEMPLATE' ) || !PODS_DISABLE_DYNAMIC_TEMPLATE ) && is_object( $pods ) && !is_wp_error( $pods ) && isset( $pods->page_template ) && !empty( $pods->page_template ) && '' != locate_template( array( $pods->page_template ), true ) ) {
  656. $template = $pods->page_template;
  657. // found the template and included it, we're good to go!
  658. }
  659. elseif ( !empty( self::$exists[ 'page_template' ] ) && '' != locate_template( array( self::$exists[ 'page_template' ] ), true ) ) {
  660. $template = self::$exists[ 'page_template' ];
  661. // found the template and included it, we're good to go!
  662. }
  663. else {
  664. $default_templates = array();
  665. $uri = explode( '?', self::$exists[ 'uri' ] );
  666. $uri = explode( '#', $uri[ 0 ] );
  667. $page_path = explode( '/', $uri[ 0 ] );
  668. while ( $last = array_pop( $page_path ) ) {
  669. $file_name = str_replace( '*', '-w-', implode( '/', $page_path ) . '/' . $last );
  670. $sanitized = sanitize_title( $file_name );
  671. $default_templates[] = 'pods-' . trim( str_replace( '--', '-', $sanitized ), ' -' ) . '.php';
  672. }
  673. $default_templates[] = 'pods.php';
  674. $default_templates = apply_filters( 'pods_page_default_templates', $default_templates );
  675. $template = locate_template( $default_templates, true );
  676. if ( '' != $template ) {
  677. // found the template and included it, we're good to go!
  678. }
  679. else {
  680. $template = false;
  681. // templates not found in theme, default output
  682. do_action( 'pods_page_default', $template, self::$exists );
  683. get_header();
  684. pods_content();
  685. get_sidebar();
  686. get_footer();
  687. }
  688. }
  689. do_action( 'pods_page_end', $template, self::$exists );
  690. exit;
  691. }
  692. }
  693. }
  694. /**
  695. * Find out if the current page is a Pod Page
  696. *
  697. * @param string $uri The Pod Page URI to check if currently on
  698. *
  699. * @return bool
  700. * @since 1.7.5
  701. */
  702. function is_pod_page ( $uri = null ) {
  703. if ( false !== Pods_Pages::$exists && ( null === $uri || $uri == Pods_Pages::$exists[ 'uri' ] || $uri == Pods_Pages::$exists[ 'id' ] ) )
  704. return true;
  705. return false;
  706. }
  707. /**
  708. * Check to see if Pod Page exists and return data
  709. *
  710. * $uri not required, if NULL then returns REQUEST_URI matching Pod Page
  711. *
  712. * @param string $uri The Pod Page URI to check if exists
  713. *
  714. * @return array
  715. */
  716. function pod_page_exists ( $uri = null ) {
  717. return Pods_Pages::exists( $uri );
  718. }
  719. /**
  720. * Output Pod Page Content
  721. *
  722. * @param bool $return Whether to return or not (default is to echo)
  723. *
  724. * @return string
  725. */
  726. function pods_content ( $return = false ) {
  727. return Pods_Pages::content( $return );
  728. }
  729. /*
  730. * Deprecated global variable
  731. */
  732. $GLOBALS[ 'pod_page_exists' ] =& Pods_Pages::$exists;