Skip to content

Feature/queryvars support #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions framework/Objects/Taxonomy.php
Original file line number Diff line number Diff line change
@@ -90,6 +90,12 @@ abstract class Taxonomy {
*/
protected $has_admin_menu = true;

/**
* Show taxonomy in REST
*
* @var bool
*/
protected $show_in_rest = true;

/**
* Optional properties to make full compatibility with standard taxonomy registration
@@ -193,6 +199,7 @@ public function register() {
'show_ui' => $this->has_admin_menu,
'show_admin_column' => $this->admin_posts_column,
'show_in_quick_edit' => $this->is_quick_editable,
'show_in_rest' => $this->show_in_rest,
'query_var' => $this->query_var,
'rewrite' => array(
'slug' => $this::$SLUG,
164 changes: 164 additions & 0 deletions framework/Supports/QueryVars.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php


namespace tcs\Theme\Supports;

/**
* Class QueryVars
*
* @package JCWP\Theme\Supports
*/
class QueryVars {

/**
* Query data
*
* @var array $query_data
*/
protected $query_data = array(
'get' => array(),
'post' => array(),
);

/**
* Query vars
*
* @var array $query_vars
*/
protected $query_vars = array();

/**
* Nonce name
*
* @var string $nonce_name
*/
private $nonce_name;

/**
* Nonce action
*
* @var string $nonce_action
*/
private $nonce_action;

/**
* Enable or disable sanitizing
*
* @var bool $sanitizing
*/
private $sanitizing = true;

/**
* QueryVars constructor.
*/
public function __construct() {
}

/**
* Configure
*
* @param $nonce_name
* @param $nonce_action
*/
public function configure( $nonce_name, $nonce_action ) {
if ( ! empty( $nonce_name ) && ! empty( $nonce_action ) ) {
$this->nonce_name = $nonce_name;
$this->nonce_action = $nonce_action;
}
}

/**
* Use_sanitize
*
* @return bool
*/
public function no_sanitize(): bool {
return $this->sanitizing = false;
}

/**
* Get query vars
*
* @param string $method Methods: post, get.
* @param string $var .
*
* @return mixed
*/
public function get_query_vars( $method = 'any', $var = '' ) {
$this->verify_query_vars();

if ( 'any' !== $method && empty( $var ) ) {
return $this->query_data[ $method ];
}

if ( 'any' !== $method && ! empty( $var ) ) {
if ( ! isset( $this->query_data[ $method ] ) || ! isset( $this->query_data[ $method ][ $var ] ) ) {
return null;
}

return $this->query_data[ $method ][ $var ];
}

return $this->query_data;
}

/**
* Add dynamic query var
*
* @param string|array $var .
*
* @return array
*/
public function add_query_var( $var ): array {
if ( ! empty( $var ) && ! is_array( $var ) && ! in_array( $var, $this->query_vars, true ) ) {
$this->query_vars[] = $var;
}

if ( ! empty( $var ) && is_array( $var ) ) {
foreach ( $var as $item ) {
if ( ! in_array( $item, $this->query_vars, true ) ) {
$this->query_vars[] = $item;
}
}
}

return $this->query_vars;
}

/**
* Verify vars
*
* @return array
*/
protected function verify_query_vars(): array {
$methods = array(
'post' => $_POST,
'get' => $_GET,
);

$_query_data = array();

foreach ( $methods as $method => $_vars ) {
$_query_data[ $method ] = array();

if ( isset( $_vars[ $this->nonce_name ] ) ) {
if ( ! wp_verify_nonce( $_vars[ $this->nonce_name ], $this->nonce_action ) ) {
$_query_data = array( $method => 'nonce error' );
continue;
}
}

foreach ( $this->query_vars as $query_var ) {
if ( empty( $_vars[ $query_var ] ) ) {
continue;
}

$_query_data[ $method ][ $query_var ] =
true === $this->sanitizing ? sanitize_text_field( $_vars[ $query_var ] ) : $_vars[ $query_var ];
}
}

return $this->query_data = $_query_data;
}

}