Last active
June 3, 2022 10:18
-
-
Save MjHead/08aa10ded2e5427040ac230912688e6b to your computer and use it in GitHub Desktop.
JSF dynamic query args prototype
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Base arg class. all args must extend it | |
abstract class Base_Arg { | |
// returns main arg | |
abstract public function get_arg(); | |
// returns main arg label for UI control | |
abstract public function get_name(); | |
// return additional arguments if needed | |
public function get_extra_args() { | |
return []; | |
} | |
// return main and additiona arguments delimiter if needed | |
public function get_delimiter() { | |
return '' | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// manager class, contain data about all args to pass it into JS component | |
class Args_Manager { | |
private $_args = []; | |
public function __construct() { | |
add_action( 'init', array( $this, 'register_args' ) ); | |
} | |
public function register_args() { | |
require 'base-arg.php'; | |
do_action( 'prefix/register-dynamic-arg', $this ); | |
} | |
public function register_arg( $arg_instance ) { | |
$this->_args[ $arg_instance->get_arg() ] = $arg_instance; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Class demonstrates example of custom query argument with additional data | |
class My_Arg extends Base_Arg { | |
public function get_arg() { | |
return 'related_children'; | |
} | |
public function get_name() { | |
return 'Filter by related children'; | |
} | |
public function get_extra_args() { | |
return [ | |
'rel_id' => [ | |
'label' => 'Select Relation', | |
'description' => 'select relation to filter by children items from', | |
'type' => 'select', // allowed types - text and select | |
'options' => [ | |
[ | |
'value' => 1, | |
'label' => 'rel 1', | |
], | |
[ | |
'value' => 2, | |
'label' => 'rel 2', | |
], | |
], | |
], | |
]; | |
} | |
public function get_delimiter() { | |
return '*' | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Class demonstrates logic of registering custom arg | |
class Register_My_Arg { | |
public function __construct() { | |
// hook from dynamic-args-manager.php | |
add_action( 'prefix/register-dynamic-arg', array( $this, 'register_arg' ) ); | |
} | |
public function register_arg( $manager ) { | |
require 'my-arg.php'; | |
$manager->register_arg( new My_Arg() ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment