Buffer_Output.php
4 years ago
Buffer_Output_Interface.php
4 years ago
Buffer_Output_Tag.php
4 years ago
Buffer_Output_Tag_Interface.php
4 years ago
Buffer_Output.php
66 lines
| 1 | <?php |
| 2 | |
| 3 | namespace cybot\cookiebot\lib\buffer; |
| 4 | |
| 5 | class Buffer_Output implements Buffer_Output_Interface { |
| 6 | |
| 7 | /** |
| 8 | * Hook tag names |
| 9 | * |
| 10 | * @var array |
| 11 | * |
| 12 | * @since 1.2.0 |
| 13 | */ |
| 14 | private $tags = array(); |
| 15 | |
| 16 | /** |
| 17 | * @param $tag_name string Hook name |
| 18 | * @param $priority integer Hook priority |
| 19 | * @param array $keywords array List of words to search for in the script |
| 20 | * @param boolean $use_cache Use Cache |
| 21 | * |
| 22 | * @since 1.2.0 |
| 23 | */ |
| 24 | public function add_tag( $tag_name, $priority, $keywords = array(), $use_cache = true ) { |
| 25 | $tag = new Buffer_Output_Tag( $tag_name, $priority, $keywords, $use_cache ); |
| 26 | $unique_id = $tag->tag . '_' . $tag->priority; |
| 27 | |
| 28 | /** |
| 29 | * If tag_name and priority exists |
| 30 | * Then merge the keywords |
| 31 | */ |
| 32 | if ( isset( $this->tags[ $unique_id ] ) ) { |
| 33 | $this->tags[ $unique_id ]->merge_keywords( $keywords ); |
| 34 | |
| 35 | if ( ! $use_cache ) { |
| 36 | $this->tags[ $unique_id ]->set_use_cache( false ); |
| 37 | } |
| 38 | } else { |
| 39 | $this->tags[ $unique_id ] = $tag; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Process every tag |
| 45 | * |
| 46 | * @since 1.2.0 |
| 47 | */ |
| 48 | public function run_actions() { |
| 49 | foreach ( $this->tags as $tag ) { |
| 50 | add_action( $tag->tag, array( $tag, 'cookiebot_start_buffer' ), $tag->priority - 1 ); |
| 51 | add_action( $tag->tag, array( $tag, 'cookiebot_stop_buffer' ), $tag->priority + 1 ); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns true if tags has more than 0 item |
| 57 | * |
| 58 | * @return bool |
| 59 | * |
| 60 | * @since 1.2.0 |
| 61 | */ |
| 62 | public function has_action() { |
| 63 | return count( $this->tags ) > 0; |
| 64 | } |
| 65 | } |
| 66 |