← All changes
|
includes/frontend/dynamic-content/interpreters/request-params.php
+51
-0
2.7.5
→
2.14.0
View file →
| @@ -1,0 +1,51 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks\Frontend\DynamicContent\Interpreters; | |
| 3 | + | |
| 4 | +use ABlocks\Helper; | |
| 5 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 6 | + exit; | |
| 7 | +} | |
| 8 | +class RequestParams extends Abstracts\Interpreter { | |
| 9 | + protected string $field; | |
| 10 | + protected string $param; | |
| 11 | + protected string $pre; | |
| 12 | + protected string $post; | |
| 13 | + protected string $default; | |
| 14 | + | |
| 15 | + public function __construct( string $name, array $setting, bool $is_richtext = false ) { | |
| 16 | + parent::__construct( $name, $setting, $is_richtext ); | |
| 17 | + $this->field = strtolower( strval( $this->setting[0] ?? 'get' ) ); | |
| 18 | + $this->param = strval( $this->setting[1] ?? '' ); | |
| 19 | + $this->pre = strval( $this->setting[3] ?? '' ); | |
| 20 | + $this->post = strval( $this->setting[4] ?? '' ); | |
| 21 | + $this->default = strval( $this->setting[5] ?? '' ); | |
| 22 | + $this->field = empty( $this->field ) ? 'get' : $this->field; | |
| 23 | + } | |
| 24 | + public function content() : string { | |
| 25 | + if ( ! is_admin() ) { | |
| 26 | + switch ( $this->field ) { | |
| 27 | + case 'get': | |
| 28 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 29 | + $this->content = $this->req_param_value( $_GET ); | |
| 30 | + break; | |
| 31 | + | |
| 32 | + case 'post': | |
| 33 | + // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 34 | + $this->content = $this->req_param_value( $_POST ); | |
| 35 | + break; | |
| 36 | + | |
| 37 | + case 'query var': | |
| 38 | + $this->content = get_query_var( $this->param, '' ); | |
| 39 | + break; | |
| 40 | + | |
| 41 | + default: | |
| 42 | + $this->content = ''; | |
| 43 | + } | |
| 44 | + } | |
| 45 | + | |
| 46 | + return $this->pre . ( $this->content === '' ? $this->default : $this->content ) . $this->post; | |
| 47 | + } | |
| 48 | + public function req_param_value( array $params ) : string { | |
| 49 | + return sanitize_text_field( $params[ $this->param ] ?? '' ); | |
| 50 | + } | |
| 51 | +} | |