/lib/PageShortcodesPlugin.php

https://github.com/dflydev/page-shortcodes · PHP · 228 lines · 144 code · 24 blank · 60 comment · 21 complexity · 7df2466dbc5182376236c2a587beb044 MD5 · raw file

  1. <?php
  2. /**
  3. * Page Shortcodes WordPress Plugin
  4. */
  5. /**
  6. * Page Shortcodes WordPress Plugin
  7. */
  8. class PageShortcodesPlugin {
  9. /**
  10. * Our instance.
  11. * @var PageShortcodesPlugin
  12. */
  13. static private $INSTANCE = null;
  14. /**
  15. * Cache pages by ID.
  16. * @var array
  17. */
  18. static private $CACHE_BY_ID = array();
  19. /**
  20. * Cache pages by ID.
  21. * @var array
  22. */
  23. static private $CACHE_BY_NAME = array();
  24. /**
  25. * Plugin directory.
  26. * @var unknown_type
  27. */
  28. private $pluginDir = null;
  29. /**
  30. * Constructor.
  31. */
  32. private function __construct($pluginFile) {
  33. $this->pluginDir = dirname($pluginFile);
  34. add_action('init', array($this, 'handleWpAction_init'));
  35. }
  36. /**
  37. * Cache a page
  38. * @param std_object $page
  39. */
  40. protected function cache($page = null) {
  41. if ( $page ) {
  42. self::$CACHE_BY_ID[$page->ID] = $page;
  43. self::$CACHE_BY_NAME[$page->post_name] = $page;
  44. }
  45. }
  46. /**
  47. * Find a page
  48. * @param array $atts Shortcode attributes
  49. * @param mixed $content Fallback content
  50. */
  51. protected function findPage($atts, $content = null) {
  52. extract(shortcode_atts(array(
  53. 'id' => null,
  54. 'name' => null,
  55. ), $atts));
  56. $foundPage = null;
  57. if ( $id ) {
  58. if ( isset(self::$CACHE_BY_ID[$id]) ) return $CACHE_BY_ID[$id];
  59. $theQuery = new WP_Query(array(
  60. 'post_type' => 'page',
  61. 'page_id' => $id,
  62. ));
  63. if ( count($theQuery->posts) ) {
  64. $foundPage = $theQuery->posts[0];
  65. }
  66. }
  67. elseif ( $name ) {
  68. if ( isset(self::$CACHE_BY_NAME[$name]) ) return self::$CACHE_BY_NAME[$name];
  69. $foundPage = get_page_by_path($name);
  70. }
  71. if ( $foundPage ) $this->cache($foundPage);
  72. return $foundPage;
  73. }
  74. /**
  75. * Handle the WordPress 'page_title' shortcode.
  76. */
  77. function handleWpShortcode_page_title($atts, $content = null) {
  78. $page = $this->findPage($atts, $content);
  79. if ( $page ) return $page->post_title;
  80. return $content;
  81. }
  82. /**
  83. * Handle the WordPress 'page_content' shortcode.
  84. */
  85. function handleWpShortcode_page_content($atts, $content = null) {
  86. $page = $this->findPage($atts, $content);
  87. if ( $page ) return $page->post_content;
  88. return $content;
  89. }
  90. /**
  91. * Handle the WordPress 'page_permalink' shortcode.
  92. */
  93. function handleWpShortcode_page_permalink($atts, $content = null) {
  94. $page = $this->findPage($atts, $content);
  95. if ( $page ) return get_permalink($page->ID);
  96. return $content;
  97. }
  98. /**
  99. * Handle the WordPress 'page_meta' shortcode.
  100. */
  101. function handleWpShortcode_page_meta($atts, $content = null) {
  102. extract(shortcode_atts(array(
  103. 'id' => null,
  104. 'name' => null,
  105. 'meta' => null,
  106. 'template' => null,
  107. ), $atts));
  108. $page = $this->findPage(array('id' => $id, 'name' => $name), $content);
  109. if ( $page ) {
  110. $data = get_post_meta($page->ID,$meta);
  111. $templates = array();
  112. if ( $template ) {
  113. $templates = $this->createTemplateSearchPath($page, $template, $templates);
  114. }
  115. $templates = $this->createTemplateSearchPath($page, 'psp-page-meta', $templates);
  116. $foundTemplate = locate_template($templates);
  117. if ( ! $foundTemplate ) {
  118. $foundTemplate = $this->localTemplate('psp-page-meta.php');
  119. }
  120. return $this->includeTemplate($foundTemplate, array(
  121. 'pspPageMeta' => isset($data[0]) ? $data[0] : null,
  122. ));
  123. }
  124. return $content;
  125. }
  126. /**
  127. * Handle the WordPress 'page_list' shortcode.
  128. */
  129. function handleWpShortcode_page_list($atts, $content = null) {
  130. extract(shortcode_atts(array(
  131. 'id' => null,
  132. 'name' => null,
  133. 'template' => null,
  134. ), $atts));
  135. if ( $name ) {
  136. $page = $this->findPage(array('name' => $name));
  137. if ( $page ) $id = $page->ID;
  138. }
  139. if ( ! $id ) return $content;
  140. $queryArgs = array('post_type' => 'page', 'post_parent' => $id, 'orderby' => 'title', 'order' => 'ASC', );
  141. $theQuery = new WP_Query($queryArgs);
  142. $posts = $theQuery->posts;
  143. if ( count($posts) ) {
  144. foreach ( $posts as $foundPage ) $this->cache($foundPage);
  145. }
  146. $output = '';
  147. $templates = array();
  148. if ( $template ) {
  149. $templates = $this->createTemplateSearchPath($page, $template, $templates);
  150. }
  151. $templates = $this->createTemplateSearchPath($page, 'psp-page-list', $templates);
  152. $foundTemplate = locate_template($templates);
  153. if ( ! $foundTemplate ) {
  154. $foundTemplate = $this->localTemplate('psp-page-list.php');
  155. }
  156. return $this->includeTemplate($foundTemplate, array(
  157. 'pspPageList' => $posts,
  158. ));
  159. }
  160. protected function createTemplateSearchPath($page, $templateName, $templates = null) {
  161. if ( $templates === null ) $templates = array();
  162. $templates[] = implode('-', array('page', $page->post_name, $templateName . '.php'));
  163. $templates[] = implode('-', array('page', $page->ID, $templateName . '.php'));
  164. $templates[] = implode('-', array('page', $templateName . '.php'));
  165. $templates[] = $templateName . '.php';
  166. return $templates;
  167. }
  168. protected function localTemplate($template) {
  169. return implode('/', array($this->pluginDir, 'templates', $template));
  170. }
  171. /**
  172. * Include a template.
  173. * @param string $template
  174. * @param array $data
  175. */
  176. protected function includeTemplate($template, $data = null){
  177. if ( $data ) extract( $data, EXTR_SKIP );
  178. ob_start();
  179. include $template;
  180. $output = ob_get_contents();
  181. ob_end_clean();
  182. return $output;
  183. }
  184. /**
  185. * Handle the WordPress 'init' action.
  186. */
  187. public function handleWpAction_init() {
  188. add_shortcode('page_permalink', array($this, 'handleWpShortcode_page_permalink'));
  189. add_shortcode('page_title', array($this, 'handleWpShortcode_page_title'));
  190. add_shortcode('page_content', array($this, 'handleWpShortcode_page_content'));
  191. add_shortcode('page_meta', array($this, 'handleWpShortcode_page_meta'));
  192. add_shortcode('page_list', array($this, 'handleWpShortcode_page_list'));
  193. }
  194. /**
  195. * We only ever want one instance of our plugin loaded.
  196. */
  197. static public function SINGLETON($pluginFile) {
  198. if ( self::$INSTANCE === null ) {
  199. self::$INSTANCE = new PageShortcodesPlugin($pluginFile);
  200. }
  201. return self::$INSTANCE;
  202. }
  203. }
  204. ?>