class-entity.php
1 year ago
class-extras.php
1 year ago
class-wrapper.php
1 year ago
index.php
2 years ago
class-wrapper.php
94 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Traits Wrapper. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.48.2 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Traits; |
| 11 | |
| 12 | use AdvancedAds\Framework\Utilities\HTML; |
| 13 | |
| 14 | defined( 'ABSPATH' ) || exit; |
| 15 | |
| 16 | /** |
| 17 | * Traits Wrapper. |
| 18 | */ |
| 19 | trait Wrapper { |
| 20 | |
| 21 | /** |
| 22 | * Get the wrapper ID for the entity. |
| 23 | * |
| 24 | * @param string $context What the value is for. Valid values are view and edit. |
| 25 | * |
| 26 | * @return string |
| 27 | */ |
| 28 | public function get_wrapper_id( $context = 'view' ): string { |
| 29 | return (string) $this->get_prop( 'wrapper-id', $context ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Get the wrapper classes for the entity. |
| 34 | * |
| 35 | * @param string $context What the value is for. Valid values are view and edit. |
| 36 | * |
| 37 | * @return string |
| 38 | */ |
| 39 | public function get_wrapper_class( $context = 'view' ): string { |
| 40 | return $this->get_prop( 'wrapper-class', $context ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Set wrapper id. |
| 45 | * |
| 46 | * @param string $wrapper_id Entity wrapper id. |
| 47 | * |
| 48 | * @return void |
| 49 | */ |
| 50 | public function set_wrapper_id( $wrapper_id ): void { |
| 51 | $this->set_prop( 'wrapper-id', sanitize_key( $wrapper_id ) ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Set wrapper class. |
| 56 | * |
| 57 | * @param string $wrapper_class Entity wrapper class. |
| 58 | * |
| 59 | * @return void |
| 60 | */ |
| 61 | public function set_wrapper_class( $wrapper_class ): void { |
| 62 | $this->set_prop( 'wrapper-class', sanitize_text_field( $wrapper_class ) ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Creates a wrapper element with the specified tag, attributes, and content. |
| 67 | * |
| 68 | * @param string $tag The HTML tag for the wrapper element. |
| 69 | * @param array $attrs Optional. An array of attributes to add to the wrapper element. Default is an empty array. |
| 70 | * @param string $content Optional. The content to be placed inside the wrapper element. Default is an empty string. |
| 71 | * |
| 72 | * @return string The generated wrapper element. |
| 73 | */ |
| 74 | public function create_wrapper( $tag, $attrs = [], $content = '' ) { |
| 75 | $attrs = HTML::build_attributes( $attrs ); |
| 76 | |
| 77 | return "<{$tag} {$attrs}>{$content}</{$tag}>"; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get the wrapper attributes. |
| 82 | * |
| 83 | * @return array |
| 84 | */ |
| 85 | public function get_wrapper_attributes(): array { |
| 86 | $attrs = []; |
| 87 | |
| 88 | $attrs['id'] = $this->get_wrapper_id(); |
| 89 | $attrs['class'] = $this->get_wrapper_class(); |
| 90 | |
| 91 | return $attrs; |
| 92 | } |
| 93 | } |
| 94 |