buffer-output-interface.php
7 years ago
buffer-output-tag-interface.php
7 years ago
buffer-output-tag.php
7 years ago
buffer-output.php
6 years ago
buffer-output-tag.php
136 lines
| 1 | <?php |
| 2 | |
| 3 | namespace cookiebot_addons\lib\buffer; |
| 4 | |
| 5 | class Buffer_Output_Tag implements Buffer_Output_Tag_Interface { |
| 6 | |
| 7 | /** |
| 8 | * Hook priority |
| 9 | * |
| 10 | * @var integer |
| 11 | * |
| 12 | * @since 1.1.0 |
| 13 | */ |
| 14 | public $priority; |
| 15 | |
| 16 | /** |
| 17 | * Hook tag name |
| 18 | * |
| 19 | * @var string |
| 20 | * |
| 21 | * @since 1.2.0 |
| 22 | */ |
| 23 | public $tag; |
| 24 | |
| 25 | /** |
| 26 | * Keywords to allow in the scripts |
| 27 | * |
| 28 | * @var array array |
| 29 | * |
| 30 | * @since 1.2.0 |
| 31 | */ |
| 32 | private $keywords; |
| 33 | |
| 34 | /** |
| 35 | * Transient unique name |
| 36 | * |
| 37 | * @var string |
| 38 | * |
| 39 | * @since 1.1.0 |
| 40 | */ |
| 41 | private $transient_name; |
| 42 | |
| 43 | /** |
| 44 | * |
| 45 | * Use transient cache |
| 46 | * |
| 47 | * @var boolean |
| 48 | * |
| 49 | * @since 1.2.0 |
| 50 | */ |
| 51 | private $use_cache; |
| 52 | |
| 53 | /** |
| 54 | * Cookiebot_Buffer_Output_Tag constructor. |
| 55 | * |
| 56 | * @param $tag |
| 57 | * @param $priority |
| 58 | * @param array $keywords |
| 59 | * @param boolean $use_cache |
| 60 | * |
| 61 | * @since 1.2.0 |
| 62 | */ |
| 63 | public function __construct( $tag, $priority, $keywords = array(), $use_cache = true ) { |
| 64 | $this->tag = $tag; |
| 65 | $this->priority = $priority; |
| 66 | $this->keywords = $keywords; |
| 67 | $this->use_cache = $use_cache; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Merges new keywords in existence keywords variable |
| 72 | * |
| 73 | * @param $keywords |
| 74 | * |
| 75 | * @since 1.2.0 |
| 76 | */ |
| 77 | public function merge_keywords( $keywords ) { |
| 78 | $this->keywords = array_merge( $this->keywords, $keywords ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Start reading the buffer/output |
| 83 | * |
| 84 | * @since 1.1.0 |
| 85 | */ |
| 86 | public function cookiebot_start_buffer() { |
| 87 | ob_start( array( $this, 'manipulate_script' ) ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Stop reading the output and output buffered data through manipulate script filter. |
| 92 | * |
| 93 | * @since 1.1.0 |
| 94 | */ |
| 95 | public function cookiebot_stop_buffer() { |
| 96 | ob_end_flush(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Manipulate google analytic scripts to cookiebot and return it back |
| 101 | * |
| 102 | * @param $buffer |
| 103 | * |
| 104 | * @return null|string|string[] |
| 105 | * |
| 106 | * @since 1.1.0 |
| 107 | */ |
| 108 | public function manipulate_script( $buffer ) { |
| 109 | /** |
| 110 | * Get wp head scripts from the cache |
| 111 | */ |
| 112 | if( $this->use_cache ) { |
| 113 | $updated_scripts = get_transient( $this->transient_name ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * If cache is not set then build it |
| 118 | */ |
| 119 | if ( !$this->use_cache || $updated_scripts === false ) { |
| 120 | /** |
| 121 | * Get all scripts and add cookieconsent if it does match with the criterion |
| 122 | */ |
| 123 | $updated_scripts = cookiebot_addons_manipulate_script( $buffer, $this->keywords ); |
| 124 | |
| 125 | if( $this->use_cache ) { |
| 126 | /** |
| 127 | * Set cache for 15 minutes |
| 128 | */ |
| 129 | set_transient( $this->transient_name, $updated_scripts, 60 * 15 ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return $updated_scripts; |
| 134 | } |
| 135 | } |
| 136 |