| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPGraphQL\Model; |
| 4 |
|
| 5 |
use GraphQLRelay\Relay; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Taxonomy - Models data for taxonomies |
| 9 |
* |
| 10 |
* @property string $description |
| 11 |
* @property ?string $graphqlPluralName |
| 12 |
* @property ?string $graphqlSingleName |
| 13 |
* @property bool $hierarchical |
| 14 |
* @property ?string $id |
| 15 |
* @property ?string $label |
| 16 |
* @property ?string $name |
| 17 |
* @property string[]|null $object_type |
| 18 |
* @property bool $public |
| 19 |
* @property ?string $restBase |
| 20 |
* @property ?string $restControllerClass |
| 21 |
* @property bool $showCloud |
| 22 |
* @property bool $showInAdminColumn |
| 23 |
* @property ?bool $showInGraphql |
| 24 |
* @property bool $showInMenu |
| 25 |
* @property bool $showInNavMenus |
| 26 |
* @property bool $showInQuickEdit |
| 27 |
* @property bool $showInRest |
| 28 |
* @property bool $showUi |
| 29 |
* |
| 30 |
* Aliases: |
| 31 |
* @property ?string $graphql_plural_name |
| 32 |
* @property ?string $graphql_single_name |
| 33 |
* |
| 34 |
* @package WPGraphQL\Model |
| 35 |
* |
| 36 |
* @extends \WPGraphQL\Model\Model<\WP_Taxonomy> |
| 37 |
*/ |
| 38 |
class Taxonomy extends Model { |
| 39 |
/** |
| 40 |
* Taxonomy constructor. |
| 41 |
* |
| 42 |
* @param \WP_Taxonomy $taxonomy The incoming Taxonomy to model. |
| 43 |
*/ |
| 44 |
public function __construct( \WP_Taxonomy $taxonomy ) { |
| 45 |
$this->data = $taxonomy; |
| 46 |
|
| 47 |
$allowed_restricted_fields = [ |
| 48 |
'id', |
| 49 |
'name', |
| 50 |
'description', |
| 51 |
'hierarchical', |
| 52 |
'object_type', |
| 53 |
'restBase', |
| 54 |
'graphql_single_name', |
| 55 |
'graphqlSingleName', |
| 56 |
'graphql_plural_name', |
| 57 |
'graphqlPluralName', |
| 58 |
'showInGraphql', |
| 59 |
'isRestricted', |
| 60 |
]; |
| 61 |
|
| 62 |
$capability = isset( $this->data->cap->edit_terms ) ? $this->data->cap->edit_terms : 'edit_terms'; |
| 63 |
|
| 64 |
parent::__construct( $capability, $allowed_restricted_fields ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* {@inheritDoc} |
| 69 |
*/ |
| 70 |
protected function is_private() { |
| 71 |
if ( false === $this->data->public && ( ! isset( $this->data->cap->edit_terms ) || ! current_user_can( $this->data->cap->edit_terms ) ) ) { |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* {@inheritDoc} |
| 80 |
*/ |
| 81 |
protected function init() { |
| 82 |
if ( empty( $this->fields ) ) { |
| 83 |
$this->fields = [ |
| 84 |
'description' => function () { |
| 85 |
return ! empty( $this->data->description ) ? $this->data->description : ''; |
| 86 |
}, |
| 87 |
'graphqlPluralName' => function () { |
| 88 |
return ! empty( $this->data->graphql_plural_name ) ? $this->data->graphql_plural_name : null; |
| 89 |
}, |
| 90 |
'graphqlSingleName' => function () { |
| 91 |
return ! empty( $this->data->graphql_single_name ) ? $this->data->graphql_single_name : null; |
| 92 |
}, |
| 93 |
'hierarchical' => function () { |
| 94 |
return true === $this->data->hierarchical; |
| 95 |
}, |
| 96 |
'id' => function () { |
| 97 |
return ! empty( $this->name ) ? Relay::toGlobalId( 'taxonomy', $this->name ) : null; |
| 98 |
}, |
| 99 |
'label' => function () { |
| 100 |
return ! empty( $this->data->label ) ? $this->data->label : null; |
| 101 |
}, |
| 102 |
'name' => function () { |
| 103 |
return ! empty( $this->data->name ) ? $this->data->name : null; |
| 104 |
}, |
| 105 |
'object_type' => function () { |
| 106 |
return ! empty( $this->data->object_type ) ? $this->data->object_type : null; |
| 107 |
}, |
| 108 |
'public' => function () { |
| 109 |
// @todo this is a bug |
| 110 |
return ! empty( $this->data->public ) ? (bool) $this->data->public : true; |
| 111 |
}, |
| 112 |
'restBase' => function () { |
| 113 |
return ! empty( $this->data->rest_base ) ? $this->data->rest_base : null; |
| 114 |
}, |
| 115 |
'restControllerClass' => function () { |
| 116 |
return ! empty( $this->data->rest_controller_class ) ? $this->data->rest_controller_class : null; |
| 117 |
}, |
| 118 |
'showCloud' => function () { |
| 119 |
return true === $this->data->show_tagcloud; |
| 120 |
}, |
| 121 |
'showInAdminColumn' => function () { |
| 122 |
return true === $this->data->show_admin_column; |
| 123 |
}, |
| 124 |
'showInGraphql' => function () { |
| 125 |
return true === $this->data->show_in_graphql; |
| 126 |
}, |
| 127 |
'showInMenu' => function () { |
| 128 |
return true === $this->data->show_in_menu; |
| 129 |
}, |
| 130 |
'showInNavMenus' => function () { |
| 131 |
return true === $this->data->show_in_nav_menus; |
| 132 |
}, |
| 133 |
'showInQuickEdit' => function () { |
| 134 |
return true === $this->data->show_in_quick_edit; |
| 135 |
}, |
| 136 |
'showInRest' => function () { |
| 137 |
return true === $this->data->show_in_rest; |
| 138 |
}, |
| 139 |
'showUi' => function () { |
| 140 |
return true === $this->data->show_ui; |
| 141 |
}, |
| 142 |
|
| 143 |
// Aliases |
| 144 |
'graphql_plural_name' => function () { |
| 145 |
return $this->graphqlPluralName; |
| 146 |
}, |
| 147 |
'graphql_single_name' => function () { |
| 148 |
return $this->graphqlSingleName; |
| 149 |
}, |
| 150 |
]; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|