BlockAnchorSupportService.php
2 years ago
BlockPatternsService.php
2 years ago
BlockService.php
2 years ago
BlockServiceProvider.php
2 years ago
BlockStylesService.php
2 years ago
BlockValidationService.php
2 years ago
CartMenuIconMigrationService.php
2 years ago
CartMigrationService.php
2 years ago
FormModeSwitcherService.php
2 years ago
ProductCollectionBadgesMigrationService.php
2 years ago
ProductListMigrationService.php
2 years ago
ProductListService.php
2 years ago
ProductPriceChoicesMigrationService.php
2 years ago
ProductSelectedPriceMigrationService.php
2 years ago
ProductVariantsMigrationService.php
2 years ago
URLParamService.php
2 years ago
URLParamService.php
248 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\BlockLibrary; |
| 4 | |
| 5 | /** |
| 6 | * Provide URL params related functionality. |
| 7 | */ |
| 8 | class URLParamService { |
| 9 | /** |
| 10 | * Prefix. |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | protected $prefix = 'query'; |
| 15 | |
| 16 | /** |
| 17 | * Pagination Key. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | protected $pagination_key = 'page'; |
| 22 | |
| 23 | /** |
| 24 | * Search Key. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | protected $search_key = 'search'; |
| 29 | |
| 30 | /** |
| 31 | * Unique instance ID. |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | protected $instance_id = ''; |
| 36 | |
| 37 | /** |
| 38 | * URL. |
| 39 | * |
| 40 | * @var string|false |
| 41 | */ |
| 42 | protected $url = false; |
| 43 | |
| 44 | /** |
| 45 | * Set the prefix. |
| 46 | * |
| 47 | * @param string $prefix Prefix. |
| 48 | */ |
| 49 | public function __construct( $prefix = 'query' ) { |
| 50 | global $sc_query_id; |
| 51 | $this->prefix = $prefix; |
| 52 | $this->instance_id = $sc_query_id; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Set the block ID. |
| 57 | * |
| 58 | * @param string $prefix Prefix. |
| 59 | */ |
| 60 | public function setPrefix( $prefix ) { |
| 61 | $this->prefix = $prefix; |
| 62 | return $this; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Set the block ID. |
| 67 | * |
| 68 | * @param string $instance_id Unique instance ID. |
| 69 | */ |
| 70 | public function setInstanceId( $instance_id ) { |
| 71 | $this->instance_id = $instance_id; |
| 72 | return $this; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get a unique key for a block. |
| 77 | * |
| 78 | * @param string $name Name. |
| 79 | * @param string $instance_id Unique instance ID. |
| 80 | * |
| 81 | * @return string |
| 82 | */ |
| 83 | public function getKey( $name = '', $instance_id = '' ) { |
| 84 | $instance_id = $instance_id ? $instance_id : $this->instance_id; |
| 85 | $prefix = ! empty( $this->prefix ) ? ( $this->prefix . '-' ) : ''; |
| 86 | if ( ! $instance_id ) { |
| 87 | return trim( $prefix . strtolower( $name ), '-' ); |
| 88 | } |
| 89 | return trim( $prefix . $instance_id . '-' . strtolower( $name ), '-' ); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /** |
| 94 | * Get the filter arguments. |
| 95 | * |
| 96 | * @param string $name Name. |
| 97 | * @param string $instance_id Unique instance ID. |
| 98 | * |
| 99 | * @return array |
| 100 | */ |
| 101 | public function getArg( $name, $instance_id = '' ) { |
| 102 | $instance_id = $instance_id ? $instance_id : $this->instance_id; |
| 103 | $key = $this->getKey( $name, $instance_id ); |
| 104 | return $_GET[ $key ] ?? null; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get the current page. |
| 109 | * |
| 110 | * @param string $instance_id Unique instance ID. |
| 111 | * |
| 112 | * @return int |
| 113 | */ |
| 114 | public function getCurrentPage( $instance_id = '' ) { |
| 115 | $instance_id = $instance_id ? $instance_id : $this->instance_id; |
| 116 | $key = $this->getKey( $this->pagination_key, $instance_id ); |
| 117 | return max( 1, absint( $_GET[ $key ] ?? 1 ) ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Add a generic argument to the URL. |
| 122 | * |
| 123 | * @param string $key Key. |
| 124 | * @param string|array $value Value. |
| 125 | * @return string |
| 126 | */ |
| 127 | public function addArg( $key, $value, $instance_id = '' ) { |
| 128 | // get the instance ID. |
| 129 | $instance_id = $instance_id ? $instance_id : $this->instance_id; |
| 130 | // get the key for this filter argument. |
| 131 | $key = $this->getKey( $key, $instance_id ); |
| 132 | // make sure the value is always lowercase. |
| 133 | $value = is_array( $value ) ? array_map( 'strtolower', $value ) : strtolower( $value ); |
| 134 | // return the new URL without pagination for filtering. |
| 135 | $this->url = add_query_arg( strtolower( $key ), $value, $this->url ); |
| 136 | // return this. |
| 137 | return $this; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get the URL. |
| 142 | * |
| 143 | * @return string |
| 144 | */ |
| 145 | public function url() { |
| 146 | return $this->url; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Add a pagination argument to the URL. |
| 151 | * |
| 152 | * @param int $page Page. |
| 153 | * @param string $instance_id Unique instance ID. |
| 154 | * |
| 155 | * @return string |
| 156 | */ |
| 157 | public function addPageArg( $page, $instance_id = '' ) { |
| 158 | return $this->addArg( $this->pagination_key, $page, $instance_id ); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Add a filter argument to the URL. |
| 163 | * |
| 164 | * @param string $key Key. |
| 165 | * @param string $value Value. |
| 166 | * @param string $instance_id Unique instance ID. |
| 167 | * |
| 168 | * @return string |
| 169 | */ |
| 170 | public function addFilterArg( $key, $value, $instance_id = '' ) { |
| 171 | // get the instance ID. |
| 172 | $instance_id = $instance_id ? $instance_id : $this->instance_id; |
| 173 | |
| 174 | // get the key for this filter argument. |
| 175 | $key = $this->getKey( $key, $instance_id ); |
| 176 | |
| 177 | // get the existing filters. |
| 178 | $existing_filters = $_GET[ $key ] ?? []; |
| 179 | |
| 180 | // add the new filter. |
| 181 | $filters = array_unique( array_merge( $existing_filters, [ $value ] ) ); |
| 182 | |
| 183 | // return the new URL without pagination for filtering. |
| 184 | return remove_query_arg( |
| 185 | [ |
| 186 | $this->getKey( $this->pagination_key, $instance_id ), |
| 187 | $this->getKey( $this->search_key, $instance_id ), |
| 188 | ], |
| 189 | add_query_arg( $key, $filters, $this->url ) |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Check if a filter argument exists in the URL. |
| 195 | * |
| 196 | * @param string $key Key. |
| 197 | * @param string $value Value. |
| 198 | * @param string $instance_id Unique instance ID. |
| 199 | * |
| 200 | * @return bool |
| 201 | */ |
| 202 | public function hasFilterArg( $key, $value, $instance_id = '' ) { |
| 203 | // get the instance ID. |
| 204 | $instance_id = $instance_id ? $instance_id : $this->instance_id; |
| 205 | |
| 206 | // get the key for this filter argument. |
| 207 | $key = $this->getKey( $key, $instance_id ); |
| 208 | |
| 209 | // get the existing filters. |
| 210 | $existing_filters = $_GET[ $key ] ?? []; |
| 211 | |
| 212 | // check if the value exists in the existing filters. |
| 213 | return in_array( strval( $value ), $existing_filters, false ); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Remove a filter argument from the URL. |
| 218 | * |
| 219 | * @param string $key Key. |
| 220 | * @param string $value Value. |
| 221 | * @param string $instance_id Unique instance ID. |
| 222 | * |
| 223 | * @return string |
| 224 | */ |
| 225 | public function removeFilterArg( $key, $value, $instance_id = '' ) { |
| 226 | // get the instance ID. |
| 227 | $instance_id = $instance_id ? $instance_id : $this->instance_id; |
| 228 | |
| 229 | // get the key for this filter argument. |
| 230 | $key = $this->getKey( $key, $instance_id ); |
| 231 | |
| 232 | // get the existing filters. |
| 233 | $existing_filters = $_GET[ $key ] ?? []; |
| 234 | |
| 235 | // remove the new filter. |
| 236 | $filters = array_diff( $existing_filters, [ $value ] ); |
| 237 | |
| 238 | // return the new URL without pagination for filtering. |
| 239 | return remove_query_arg( |
| 240 | [ |
| 241 | $this->getKey( $this->pagination_key, $instance_id ), |
| 242 | $this->getKey( $this->search_key, $instance_id ), |
| 243 | ], |
| 244 | add_query_arg( $key, $filters, $this->url ) |
| 245 | ); |
| 246 | } |
| 247 | } |
| 248 |