base-source.php
1 year ago
preset-source-post.php
2 years ago
preset-source-query-var.php
2 years ago
preset-source-term.php
1 year ago
preset-source-user.php
2 years ago
preset-source-term.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Presets\Sources; |
| 4 | |
| 5 | use Jet_Form_Builder\Exceptions\Preset_Exception; |
| 6 | |
| 7 | if ( ! defined( 'WPINC' ) ) { |
| 8 | die; |
| 9 | } |
| 10 | |
| 11 | class Preset_Source_Term extends Base_Source { |
| 12 | |
| 13 | /** |
| 14 | * Get the ID of the source. |
| 15 | * |
| 16 | * @return string |
| 17 | */ |
| 18 | public function get_id() { |
| 19 | return 'term'; |
| 20 | } |
| 21 | |
| 22 | public function on_sanitize(): bool { |
| 23 | if ( ! is_user_logged_in() ) { |
| 24 | return false; |
| 25 | } |
| 26 | |
| 27 | $term_taxonomy = ! empty( $this->preset_data['term_taxonomy'] ) ? $this->preset_data['term_taxonomy'] : 'category'; |
| 28 | |
| 29 | if ( ! $this->user_can_manage_taxonomy_terms( $term_taxonomy ) ) { |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | protected function can_get_preset() { |
| 37 | $term_taxonomy = ! empty( $this->preset_data['term_taxonomy'] ) ? $this->preset_data['term_taxonomy'] : 'category'; |
| 38 | |
| 39 | return ( parent::can_get_preset() && |
| 40 | $this->user_can_manage_taxonomy_terms( $term_taxonomy ) |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Query the source term based on preset data. |
| 46 | * |
| 47 | * @return object|WP_Term |
| 48 | * @throws Preset_Exception |
| 49 | */ |
| 50 | public function query_source() { |
| 51 | $term_from = ! empty( $this->preset_data['term_from'] ) ? $this->preset_data['term_from'] : 'current_term'; |
| 52 | |
| 53 | if ( 'current_term' === $term_from ) { |
| 54 | $term = get_queried_object(); |
| 55 | } else { |
| 56 | $var = ! empty( $this->preset_data['query_var'] ) ? $this->preset_data['query_var'] : ''; |
| 57 | $term_taxonomy = ! empty( $this->preset_data['term_taxonomy'] ) ? $this->preset_data['term_taxonomy'] : 'category'; |
| 58 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 59 | $term_id = ( $var && isset( $_REQUEST[ $var ] ) ) ? absint( $_REQUEST[ $var ] ) : false; |
| 60 | |
| 61 | if ( ! $term_id ) { |
| 62 | throw new Preset_Exception( 'Empty Term ID' ); |
| 63 | } |
| 64 | |
| 65 | $term = get_term( $term_id, $term_taxonomy ); |
| 66 | } |
| 67 | |
| 68 | return $term; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get term meta data for the queried term. |
| 73 | * |
| 74 | * @return mixed |
| 75 | */ |
| 76 | public function source__term_meta() { |
| 77 | $term = $this->query_source(); |
| 78 | return get_term_meta( $term->term_id, $this->field_data['key'], true ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get the parent term of the queried term. |
| 83 | * |
| 84 | * @return object|WP_Term |
| 85 | */ |
| 86 | public function source__parent_term() { |
| 87 | $term = $this->query_source(); |
| 88 | return get_term( $term->parent, $term->taxonomy ); |
| 89 | } |
| 90 | |
| 91 | public function user_can_manage_taxonomy_terms( $taxonomy ) { |
| 92 | $taxonomy_object = get_taxonomy( $taxonomy ); |
| 93 | |
| 94 | if ( ! $taxonomy_object ) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | $capability = $taxonomy_object->cap->edit_terms; |
| 99 | |
| 100 | return current_user_can( $capability ); |
| 101 | } |
| 102 | } |
| 103 |