class-entity.php
1 year ago
class-extras.php
1 year ago
class-wrapper.php
1 week ago
index.php
2 years ago
class-wrapper.php
155 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 (string) $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 | if ( property_exists( $this, 'wrapper' ) ) { |
| 52 | $this->wrapper = null; |
| 53 | } |
| 54 | |
| 55 | $this->set_prop( 'wrapper-id', sanitize_key( $wrapper_id ) ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Set wrapper class. |
| 60 | * |
| 61 | * @param string $wrapper_class Entity wrapper class. |
| 62 | * |
| 63 | * @return void |
| 64 | */ |
| 65 | public function set_wrapper_class( $wrapper_class ): void { |
| 66 | if ( property_exists( $this, 'wrapper' ) ) { |
| 67 | $this->wrapper = null; |
| 68 | } |
| 69 | |
| 70 | $this->set_prop( 'wrapper-class', sanitize_text_field( $wrapper_class ) ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Creates a wrapper element with the specified tag, attributes, and content. |
| 75 | * |
| 76 | * @param string $tag The HTML tag for the wrapper element. |
| 77 | * @param array $attrs Optional. An array of attributes to add to the wrapper element. Default is an empty array. |
| 78 | * @param string $content Optional. The content to be placed inside the wrapper element. Default is an empty string. |
| 79 | * |
| 80 | * @return string The generated wrapper element. |
| 81 | */ |
| 82 | public function create_wrapper( $tag, $attrs = [], $content = '' ) { |
| 83 | $attrs = HTML::build_attributes( $attrs ); |
| 84 | |
| 85 | return "<{$tag} {$attrs}>{$content}</{$tag}>"; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /** |
| 90 | * Sets the wrapper styles based on the given position. |
| 91 | * |
| 92 | * @param array $wrapper The wrapper array to store the styles. |
| 93 | * @param string $position The position of the ad. |
| 94 | * @param bool $use_position Whether to use the position or not. |
| 95 | * |
| 96 | * @return void |
| 97 | */ |
| 98 | protected function get_wrapper_styles( &$wrapper, $position, $use_position = false ): void { |
| 99 | // Always keep margin before handling position (Specific to Ads). |
| 100 | if ( method_exists( $this, 'get_margin' ) ) { |
| 101 | $margin = $this->get_margin(); |
| 102 | foreach ( $margin as $key => $value ) { |
| 103 | if ( ! empty( $value ) ) { |
| 104 | $wrapper['style'][ 'margin-' . $key ] = $value . 'px'; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | switch ( $position ) { |
| 110 | case 'left': |
| 111 | case 'left_float': |
| 112 | case 'left_nofloat': |
| 113 | $wrapper['style']['float'] = 'left'; |
| 114 | break; |
| 115 | case 'right': |
| 116 | case 'right_float': |
| 117 | case 'right_nofloat': |
| 118 | $wrapper['style']['float'] = 'right'; |
| 119 | break; |
| 120 | case 'center': |
| 121 | case 'center_nofloat': |
| 122 | case 'center_float': |
| 123 | if ( method_exists( $this, 'get_margin' ) ) { |
| 124 | $wrapper['style']['margin-left'] = 'auto'; |
| 125 | $wrapper['style']['margin-right'] = 'auto'; |
| 126 | } |
| 127 | |
| 128 | $width = method_exists( $this, 'get_width' ) ? $this->get_width() : 0; |
| 129 | $add_wrapper_sizes = method_exists( $this, 'get_prop' ) ? $this->get_prop( 'add_wrapper_sizes' ) : false; |
| 130 | |
| 131 | if ( empty( $width ) || empty( $add_wrapper_sizes ) || $use_position ) { |
| 132 | $wrapper['style']['text-align'] = 'center'; |
| 133 | } |
| 134 | break; |
| 135 | case 'clearfix': |
| 136 | $wrapper['style']['clear'] = 'both'; |
| 137 | break; |
| 138 | } |
| 139 | |
| 140 | if ( method_exists( $this, 'is_space_reserved' ) && $this->is_space_reserved() ) { |
| 141 | if ( method_exists( $this, 'get_width' ) && ! empty( $this->get_width() ) ) { |
| 142 | $wrapper['style']['width'] = $this->get_width() . 'px'; |
| 143 | } |
| 144 | |
| 145 | if ( method_exists( $this, 'get_height' ) && ! empty( $this->get_height() ) ) { |
| 146 | $wrapper['style']['height'] = $this->get_height() . 'px'; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if ( method_exists( $this, 'get_clearfix' ) && ! empty( $this->get_clearfix() ) ) { |
| 151 | $wrapper['style']['clear'] = 'both'; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 |