| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPGraphQL\Model; |
| 4 |
|
| 5 |
use GraphQLRelay\Relay; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Theme - Models data for themes |
| 9 |
* |
| 10 |
* @property ?string $author |
| 11 |
* @property ?string $authorUri |
| 12 |
* @property ?string $description |
| 13 |
* @property ?string $id |
| 14 |
* @property ?string $name |
| 15 |
* @property ?string $screenshot |
| 16 |
* @property ?string $slug |
| 17 |
* @property ?string $themeUri |
| 18 |
* @property string[]|null $tags |
| 19 |
* @property ?string $version |
| 20 |
* |
| 21 |
* @package WPGraphQL\Model |
| 22 |
* |
| 23 |
* @extends \WPGraphQL\Model\Model<\WP_Theme> |
| 24 |
*/ |
| 25 |
class Theme extends Model { |
| 26 |
/** |
| 27 |
* Theme constructor. |
| 28 |
* |
| 29 |
* @param \WP_Theme $theme The incoming WP_Theme to be modeled |
| 30 |
* |
| 31 |
* @return void |
| 32 |
* @throws \Exception |
| 33 |
*/ |
| 34 |
public function __construct( \WP_Theme $theme ) { |
| 35 |
$this->data = $theme; |
| 36 |
parent::__construct(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* {@inheritDoc} |
| 41 |
*/ |
| 42 |
protected function is_private() { |
| 43 |
// Don't assume a capabilities hierarchy, since it's likely headless sites might disable some capabilities site-wide. |
| 44 |
if ( current_user_can( 'edit_themes' ) || current_user_can( 'switch_themes' ) || current_user_can( 'update_themes' ) ) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
if ( wp_get_theme()->get_stylesheet() !== $this->data->get_stylesheet() ) { |
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* {@inheritDoc} |
| 57 |
*/ |
| 58 |
protected function init() { |
| 59 |
if ( empty( $this->fields ) ) { |
| 60 |
$this->fields = [ |
| 61 |
'author' => function () { |
| 62 |
return ! empty( $this->data->author ) ? $this->data->author : null; |
| 63 |
}, |
| 64 |
'authorUri' => function () { |
| 65 |
$author_uri = $this->data->get( 'AuthorURI' ); |
| 66 |
return ! empty( $author_uri ) ? $author_uri : null; |
| 67 |
}, |
| 68 |
'description' => function () { |
| 69 |
return ! empty( $this->data->description ) ? $this->data->description : null; |
| 70 |
}, |
| 71 |
'id' => function () { |
| 72 |
return ! empty( $this->slug ) ? Relay::toGlobalId( 'theme', $this->slug ) : null; |
| 73 |
}, |
| 74 |
'name' => function () { |
| 75 |
$name = $this->data->get( 'Name' ); |
| 76 |
return ! empty( $name ) ? $name : null; |
| 77 |
}, |
| 78 |
'screenshot' => function () { |
| 79 |
$screenshot = $this->data->get_screenshot(); |
| 80 |
return ! empty( $screenshot ) ? $screenshot : null; |
| 81 |
}, |
| 82 |
'slug' => function () { |
| 83 |
$stylesheet = $this->data->get_stylesheet(); |
| 84 |
return ! empty( $stylesheet ) ? $stylesheet : null; |
| 85 |
}, |
| 86 |
'themeUri' => function () { |
| 87 |
$theme_uri = $this->data->get( 'ThemeURI' ); |
| 88 |
return ! empty( $theme_uri ) ? $theme_uri : null; |
| 89 |
}, |
| 90 |
'tags' => function () { |
| 91 |
return ! empty( $this->data->tags ) ? $this->data->tags : null; |
| 92 |
}, |
| 93 |
'version' => function () { |
| 94 |
return ! empty( $this->data->version ) ? (string) $this->data->version : null; |
| 95 |
}, |
| 96 |
]; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|