PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/cornerstone/includes/classes/common/class-front-end.php

https://bitbucket.org/callum-harrod/thestudentroom
PHP | 328 lines | 198 code | 91 blank | 39 comment | 25 complexity | b703ab664f613bdac2667cd851c0dcea MD5 | raw file
Possible License(s): GPL-2.0, ISC
  1. <?php
  2. /**
  3. * Manage all the front end code for Cornerstone
  4. * including shortcode styling and scripts
  5. */
  6. class Cornerstone_Front_End extends Cornerstone_Plugin_Component {
  7. public $dependencies = array( 'Inline_Scripts' );
  8. /**
  9. * Setup hooks
  10. */
  11. public function setup() {
  12. add_filter('template_include', array( $this, 'setup_after_template_include' ), 99999 );
  13. // Enqueue Scripts & Styles
  14. add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ) );
  15. add_action( 'wp_enqueue_scripts', array( $this, 'styles' ) );
  16. add_action( 'cs_late_template_redirect', array( $this, 'postLoaded' ), 9998, 0 );
  17. add_filter( 'get_the_excerpt', array( $this, 'maybe_supply_excerpt' ), 100 );
  18. // Add Body Class
  19. add_filter( 'body_class', array( $this, 'addBodyClass' ), 10002 );
  20. add_filter( 'the_content', array( $this, 'cs_content_before_shortcodes' ), 10 );
  21. add_filter( 'the_content', array( $this, 'cs_content_late' ), 999999 );
  22. add_shortcode( 'cs_content', array( $this, 'cs_content_shortcode' ) );
  23. add_action( 'wp_head', array( $this, 'cs_head_late'), 10000 );
  24. add_action( 'wp_head', array( $this, 'cs_head_late_after'), 10001 );
  25. add_action( 'wp_footer', array( $this, 'output_late_styles') );
  26. add_action('x_section', array( $this, 'output_layout_content') );
  27. add_action('x_row', array( $this, 'output_layout_content') );
  28. add_action('x_column', array( $this, 'output_layout_content') );
  29. add_action('cs_before_preview_frame', array( $this, 'preview_frame_setup' ) );
  30. add_action('cs_element_rendering', array( $this, 'register_scripts') );
  31. add_action( 'wp_footer', array( $this, 'shim_x_zones') ); // Needed for preview
  32. add_filter( 'script_loader_tag', array( $this, 'fix_script_tags'), 0, 3 );
  33. add_action( 'cs_the_content_late', array( $this, 'shim_x_before_site_end') );
  34. }
  35. /**
  36. * A late template_redirect hook allows plugins like Custom 404 and Under Construction
  37. * to modify the query before we assume we can query info like the current ID
  38. */
  39. public function setup_after_template_include( $template ) {
  40. do_action('cs_late_template_redirect');
  41. return $template;
  42. }
  43. /**
  44. * Enqueue Styles
  45. */
  46. public function styles() {
  47. if ( apply_filters( 'cornerstone_enqueue_styles', true ) ) {
  48. wp_enqueue_style( 'cornerstone-shortcodes', $this->plugin->css( 'site/style' ), array(), $this->plugin->version() );
  49. }
  50. if ( apply_filters( 'cornerstone_legacy_font_classes', false ) ) {
  51. wp_enqueue_style( 'x-fa-icon-classes', $this->plugin->css( 'site/fa-icon-classes' ), array(), $this->plugin->version() );
  52. }
  53. }
  54. /**
  55. * Enqueue Scripts
  56. */
  57. public function scripts() {
  58. $this->register_scripts();
  59. wp_enqueue_script( 'cornerstone-site-head' );
  60. wp_enqueue_script( 'cornerstone-site-body' );
  61. }
  62. public function register_scripts() {
  63. wp_register_script( 'cornerstone-site-head', $this->plugin->js( 'site/cs-head' ), array( 'jquery' ), $this->plugin->version(), false );
  64. wp_register_script( 'cornerstone-site-body', $this->plugin->js( 'site/cs-body' ), array( 'cornerstone-site-head' ), $this->plugin->version(), true );
  65. wp_register_script( 'vendor-ilightbox', $this->url( 'assets/dist/js/site/ilightbox.js' ), array( 'jquery' ), $this->plugin->version(), true );
  66. }
  67. public function postLoaded() {
  68. if ( apply_filters( '_cornerstone_front_end', true ) ) {
  69. add_action( 'wp_head', array( $this, 'inlineStyles' ), 9998, 0 );
  70. add_action( 'wp_footer', array( $this, 'inlineScripts' ) );
  71. }
  72. add_action( 'x_head_css', array( $this, 'output_generated_styles') );
  73. $inline_scripts = $this->plugin->component('Inline_Scripts');
  74. add_action( 'wp_footer', array( $inline_scripts, 'output_scripts' ), 9998, 0 );
  75. $this->postSettings = $this->plugin->common()->get_post_settings( get_the_ID() );
  76. }
  77. public function output_generated_styles() {
  78. echo $styling = $this->plugin->component('Styling')->get_generated_styles();
  79. }
  80. /**
  81. * Add Body class from Cornerstone Version number
  82. */
  83. public function addBodyClass( $classes ) {
  84. $classes[] = 'cornerstone-v' . str_replace( '.', '_', $this->plugin->version() );
  85. return $classes;
  86. }
  87. /**
  88. * Load generated CSS output and place style tag in wp_head
  89. */
  90. public function inlineStyles() {
  91. ob_start();
  92. if ( apply_filters( 'cornerstone_customizer_output', true ) ) {
  93. echo '<style id="cornerstone-generated-css">';
  94. $data = array_merge( $this->plugin->settings(), $this->plugin->common()->theme_integration_options() );
  95. $this->view( 'frontend/styles', true, $data, true );
  96. echo $this->plugin->component('Styling')->get_generated_styles();
  97. do_action( 'cornerstone_head_css' );
  98. echo '</style>';
  99. $custom_css = get_option( 'cs_v1_custom_css', '' );
  100. if ( $custom_css ) {
  101. echo '<style id="cornerstone-custom-css">' . $custom_css . '</style>';
  102. }
  103. }
  104. if ( is_singular() && apply_filters( '_cornerstone_custom_css', isset( $this->postSettings['custom_css'] ) ) ) {
  105. echo '<style id="cornerstone-custom-page-css">';
  106. echo $this->postSettings['custom_css'];
  107. do_action( 'cornerstone_custom_page_css' );
  108. echo '</style>';
  109. }
  110. $css = ob_get_contents(); ob_end_clean();
  111. //
  112. // 1. Remove comments.
  113. // 2. Remove whitespace.
  114. // 3. Remove starting whitespace.
  115. //
  116. $output = preg_replace( '#/\*.*?\*/#s', '', $css ); // 1
  117. $output = preg_replace( '/\s*([{}|:;,])\s+/', '$1', $output ); // 2
  118. $output = preg_replace( '/\s\s+(.*)/', '$1', $output ); // 3
  119. echo $output;
  120. }
  121. public function inlineScripts() {
  122. $inline_scripts = $this->plugin->component('Inline_Scripts');
  123. if ( apply_filters( 'cornerstone_customizer_output', true ) ) {
  124. $custom_js = get_option( 'cs_v1_custom_js', '' );
  125. if ( $custom_js ) {
  126. $inline_scripts->add_script('cornerstone-custom-js', $custom_js );
  127. }
  128. }
  129. if ( is_singular() && isset( $this->postSettings['custom_js'] ) && $this->postSettings['custom_js'] ) {
  130. $inline_scripts->add_script('cornerstone-custom-content-js', $this->postSettings['custom_js'] );
  131. }
  132. }
  133. public function maybe_supply_excerpt( $excerpt ) {
  134. if ( '' === $excerpt ) {
  135. $post = get_post();
  136. $cs_excerpt = get_post_meta( $post->ID, '_cornerstone_excerpt', true );
  137. if ( $cs_excerpt ) {
  138. return cs_format_excerpt( $cs_excerpt );
  139. }
  140. }
  141. return $excerpt;
  142. }
  143. /**
  144. * Cornerstone adds a wrapping [cs_content] shortcode.Run the content through
  145. * cs_noemptyp if we know it was originally generated by Cornerstone.
  146. * This cleans up any empty <p> tags. Next We'll manually replace this with
  147. * our wrapping div since it's much faster than adding another layer of
  148. * nested do_shortcode calls.
  149. * @param string $content Early the_content. Before do_shortcode
  150. * @return string the_content with empty <p> tags removed and wrapping div
  151. */
  152. public function cs_content_before_shortcodes( $content ) {
  153. if ( false !== strpos( $content, '[cs_content]' ) && false !== strpos( $content, '[/cs_content]' ) ) {
  154. $content = cs_noemptyp( $content );
  155. $atts = cs_atts( apply_filters( 'cs_content_atts', array(
  156. 'id' => 'cs-content',
  157. 'class' => 'cs-content',
  158. ), get_the_ID(), get_post_type() ) );
  159. $content = str_replace( '[cs_content]', "<div $atts>", $content );
  160. $content = str_replace( '[/cs_content]', '<!--cs-content-end--></div>', $content );
  161. } else {
  162. $content = str_replace( '[cs_content]', '', $content );
  163. $content = str_replace( '[/cs_content]', '', $content );
  164. }
  165. return $content;
  166. }
  167. public function cs_content_late( $content ) {
  168. if ( false !== strpos( $content, '<!--cs-content-end-->' ) ) {
  169. ob_start();
  170. do_action( 'cs_the_content_late' );
  171. $late_content = ob_get_clean();
  172. $content = apply_filters( 'cs_content_late', str_replace( '<!--cs-content-end-->', $late_content, $content ) );
  173. }
  174. return $content;
  175. }
  176. public function cs_content_shortcode( $atts, $content ) {
  177. extract( shortcode_atts( array(
  178. '_p' => false,
  179. 'no_wrap' => false,
  180. ), $atts, 'cs_content' ) );
  181. $attrs = array( 'class' => 'cs-content' );
  182. if ( $_p ) {
  183. $content = $this->plugin->component('Element_Front_End')->shortcode_output( array(
  184. '_p' => $_p
  185. ), $content, 'cs_content' );
  186. } else {
  187. $attrs['id'] = 'cs-content';
  188. }
  189. $content = do_shortcode( $content );
  190. do_action('cs_content_shortcode', $_p );
  191. if ( $no_wrap ) {
  192. return $content;
  193. }
  194. $attrs = cs_atts( $attrs );
  195. return "<div $attrs >$content</div>";
  196. }
  197. public function shim_x_before_site_end() {
  198. do_action( 'x_before_site_end' );
  199. }
  200. public function shim_x_zones() {
  201. $zones = array( 'x_before_site_end' );
  202. foreach ($zones as $action) {
  203. if ( ! did_action( $action ) ) {
  204. do_action( $action );
  205. }
  206. }
  207. }
  208. public function output_layout_content( $content ) {
  209. if ( is_scalar( $content ) ) {
  210. echo $content;
  211. }
  212. }
  213. public function preview_frame_setup() {
  214. if ( apply_filters( 'cornerstone_customizer_output', true ) ) {
  215. add_filter('cs_preview_frame_route_config', array($this, 'add_preview_styles_hook' ) );
  216. }
  217. }
  218. public function add_preview_styles_hook( $config ) {
  219. if ( ! isset( $config['dynamic_css_selector'] ) ) {
  220. $config['dynamic_css_selector'] = '#cornerstone-generated-css';
  221. }
  222. return $config;
  223. }
  224. public function cs_head_late() {
  225. do_action( 'cs_head_late' );
  226. }
  227. public function cs_head_late_after() {
  228. do_action( 'cs_head_late_after' );
  229. }
  230. public function output_late_styles() {
  231. $styling = $this->plugin->component('Styling');
  232. $styling->output_late_style( 'cs-late-element-css', $styling->get_generated_late_styles() );
  233. }
  234. public function fix_script_tags( $tag, $handle, $src ) {
  235. return $this->plugin->component('Common')->fix_script_tags( $handle, $tag );
  236. }
  237. }