PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/see-2018/waldo-master/waldo.php

https://bitbucket.org/driftcreate/supermarketetc
PHP | 167 lines | 65 code | 19 blank | 83 comment | 6 complexity | 2567ea5bb246a94f9b510d83935b9b31 MD5 | raw file
  1. <?php
  2. /**
  3. * A Dynamic Responsive CSS Background Images Extension for WordPress
  4. * and Other PHP-Based Applications
  5. *
  6. * Adds support to WordPress themes and other PHP based applications
  7. * (with appropriate modifications) to dynamically generate styles
  8. * for background images using only CSS.
  9. *
  10. * Waldo
  11. *
  12. * @package Waldo
  13. * @author Paper Leaf <info@paper-leaf.com>
  14. * @copyright 2016 Paper Leaf Design <http://www.paper-leaf.com>
  15. * @license https://github.com/paper-leaf/waldo/blob/master/LICENSE.txt GNU General Public License - Version 3
  16. * @version 1.0
  17. * @link https://github.com/paper-leaf/waldo
  18. *
  19. * Credit:
  20. * Thank you to Donald Allen <https://github.com/donaldallen> for presenting the
  21. * initial idea of PHP generated styles for use with background images.
  22. */
  23. class Waldo {
  24. /**
  25. * Constructor
  26. * Creates global class vars, assigns default values.
  27. */
  28. function __construct() {
  29. /**
  30. * $this->waldo_sizes
  31. * Sets up supported media query sizes. These should match the image sizes supported in your WordPress installation.
  32. * @var array
  33. */
  34. $this->waldo_sizes = array(
  35. 'large' => '',
  36. 'xlarge' => '1000',
  37. 'xxlarge' => '1400',
  38. 'xxxlarge' => '1800',
  39. 'xxxxlarge' => '2200'
  40. );
  41. /**
  42. * $this->template_dir
  43. * Get base path of current template. Uses WP get_template_directory() by default.
  44. * @var string
  45. */
  46. $this->template_dir = get_template_directory();
  47. }
  48. /**
  49. * waldoSavedStyles
  50. * Retrieve and unserialize styles array from file.
  51. *
  52. * @method waldoSavedStyles
  53. * @author Paper Leaf <info@paper-leaf.com>
  54. *
  55. * @return array
  56. */
  57. public function waldoSavedStyles() {
  58. return unserialize(file_get_contents($this->template_dir .'/waldo-styles.php'));
  59. }
  60. /**
  61. * waldoStylesArray
  62. * Build responsive background image styles and add to waldo_styles array.
  63. *
  64. * @method waldoStylesArray
  65. * @author Paper Leaf <info @ paper-leaf.com>
  66. *
  67. * @param array $image Configured to work with the Advanced Custom Field plugin image object by default.
  68. * @param string $section_label Unique name for the array key.
  69. * @param array $waldo_styles Array of saved styles for different sections.
  70. * @param string $image_class Must be a single, unique class name.
  71. * @return array
  72. */
  73. public function waldoStylesArray($image, $section_label, $waldo_styles, $image_class){
  74. if ( $image ) { // check if ACF image passed to $image
  75. // add array with unique key to styles array
  76. // when reloaded from server, this index will be overwritten (with new values, if applicable)
  77. foreach ( $this->waldo_sizes as $size_label => $mq_value ) {
  78. $waldo_styles[$section_label][$size_label] = "
  79. ." .$image_class ." {
  80. background-image: url('" .$image['sizes'][$size_label] ."');
  81. }
  82. ";
  83. }
  84. } else { // remove image styles if the image has been removed
  85. unset($waldo_styles[$section_label]);
  86. }
  87. return $waldo_styles;
  88. }
  89. /**
  90. * waldoStyles
  91. * Minify and write styles to waldo.css, save waldo_styles array to waldo-styles.php.
  92. *
  93. * @method waldoStyles
  94. * @author Paper Leaf <info@paper-leaf.com>
  95. *
  96. * @param array $waldo_styles Array of saved styles for different sections.
  97. * @return null
  98. */
  99. public function waldoStyles($waldo_styles){
  100. $waldo_style_content = ''; // build string to pass to waldo.css
  101. $template_dir = $this->template_dir; // locally store path to template directory
  102. if ( !empty($waldo_styles) ) { // ensure Waldo is being called in this template file
  103. foreach ( $this->waldo_sizes as $size_label => $mq_value ) {
  104. if ( $mq_value == '' ) { // no 'min-width' media query required for default image size
  105. foreach ( $waldo_styles as $waldo_style ) {
  106. $waldo_style_content .= $waldo_style[$size_label];
  107. }
  108. } else {
  109. $waldo_style_content .= "@media (min-width: " .$mq_value ."px) {";
  110. foreach ( $waldo_styles as $waldo_style ) {
  111. $waldo_style_content .= $waldo_style[$size_label];
  112. }
  113. $waldo_style_content .= "}";
  114. }
  115. }
  116. // write styles to waldo.css
  117. $waldo_style_content = preg_replace('/\s+/', '', $waldo_style_content); // comment out this line to render an un-minified style sheet
  118. file_put_contents($template_dir .'/waldo.css', $waldo_style_content);
  119. // write styles array to waldo-styles.php
  120. file_put_contents($template_dir .'/waldo-styles.php', serialize($waldo_styles)); // serialize data - file_put_contents() has issues with multidimensional arrays
  121. }
  122. return;
  123. }
  124. }
  125. /**
  126. * init_waldo
  127. * Creates a new waldo object and stores to global var $waldo. Calls $waldo->waldoSavedStyles() and stores returned array to global var $waldo_styles.
  128. *
  129. * @return null
  130. */
  131. function init_waldo() {
  132. global $waldo, $waldo_styles;
  133. $waldo = new Waldo();
  134. $waldo_styles = $waldo->waldoSavedStyles();
  135. }
  136. add_action('wp_head', 'init_waldo', 99);
  137. /**
  138. * compile_waldo
  139. * Calls $waldo->waldoStyles().
  140. *
  141. * @return null
  142. */
  143. function compile_waldo() {
  144. global $waldo, $waldo_styles;
  145. $waldo->waldoStyles($waldo_styles);
  146. }
  147. add_action('wp_footer', 'compile_waldo', 1);
  148. ?>