Fields
1 year ago
Payloads
1 year ago
SubjectTransformers
2 years ago
Subjects
5 months ago
ContextFactory.php
2 months ago
WordPressIntegration.php
2 years ago
index.php
3 years ago
ContextFactory.php
171 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\WordPress; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\WordPress; |
| 9 | |
| 10 | class ContextFactory { |
| 11 | private const ELEVATED_CAPABILITIES = [ |
| 12 | 'activate_plugins', |
| 13 | 'create_users', |
| 14 | 'delete_plugins', |
| 15 | 'delete_themes', |
| 16 | 'delete_users', |
| 17 | 'edit_files', |
| 18 | 'edit_plugins', |
| 19 | 'edit_themes', |
| 20 | 'edit_users', |
| 21 | 'install_plugins', |
| 22 | 'install_themes', |
| 23 | 'manage_network', |
| 24 | 'manage_network_options', |
| 25 | 'manage_network_plugins', |
| 26 | 'manage_network_themes', |
| 27 | 'manage_network_users', |
| 28 | 'manage_options', |
| 29 | 'manage_sites', |
| 30 | 'manage_woocommerce', |
| 31 | 'mailpoet_manage_settings', |
| 32 | 'promote_users', |
| 33 | 'remove_users', |
| 34 | 'switch_themes', |
| 35 | 'unfiltered_html', |
| 36 | 'update_core', |
| 37 | 'update_plugins', |
| 38 | 'update_themes', |
| 39 | 'upgrade_network', |
| 40 | ]; |
| 41 | |
| 42 | /** @var WordPress */ |
| 43 | private $wp; |
| 44 | |
| 45 | public function __construct( |
| 46 | WordPress $wp |
| 47 | ) { |
| 48 | $this->wp = $wp; |
| 49 | } |
| 50 | |
| 51 | /** @return mixed[] */ |
| 52 | public function getContextData(): array { |
| 53 | return [ |
| 54 | 'comment_statuses' => $this->getCommentStatuses(), |
| 55 | 'editable_roles' => $this->getEditableRoles(), |
| 56 | 'post_types' => $this->getPostTypes(), |
| 57 | 'taxonomies' => $this->getTaxonomies(), |
| 58 | ]; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @return string[][] |
| 63 | */ |
| 64 | private function getCommentStatuses(): array { |
| 65 | $statiMap = $this->wp->getCommentStatuses(); |
| 66 | $stati = []; |
| 67 | foreach ($statiMap as $id => $name) { |
| 68 | $stati[] = [ |
| 69 | 'id' => $id, |
| 70 | 'name' => $name, |
| 71 | ]; |
| 72 | } |
| 73 | return $stati; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @return string[][] |
| 78 | */ |
| 79 | private function getEditableRoles(): array { |
| 80 | $roles = []; |
| 81 | foreach ($this->wp->getEditableRoles() as $id => $role) { |
| 82 | $roleId = (string)$id; |
| 83 | if ($this->isElevatedRole($roleId, $role)) { |
| 84 | continue; |
| 85 | } |
| 86 | $roles[] = [ |
| 87 | 'id' => $roleId, |
| 88 | 'name' => (string)($role['name'] ?? $roleId), |
| 89 | ]; |
| 90 | } |
| 91 | return $roles; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @param string $id |
| 96 | * @param array{name?: string, capabilities?: array<string, bool>} $role |
| 97 | */ |
| 98 | private function isElevatedRole(string $id, array $role): bool { |
| 99 | if ($id === 'administrator') { |
| 100 | return true; |
| 101 | } |
| 102 | $capabilities = $role['capabilities'] ?? []; |
| 103 | foreach (self::ELEVATED_CAPABILITIES as $capability) { |
| 104 | if (!empty($capabilities[$capability])) { |
| 105 | return true; |
| 106 | } |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @return array<int, array<string, array<string, bool>|bool|string>> |
| 113 | */ |
| 114 | private function getPostTypes(): array { |
| 115 | /** @var \WP_Post_Type[] $postTypes */ |
| 116 | $postTypes = $this->wp->getPostTypes([], 'objects'); |
| 117 | return array_values(array_map(function(\WP_Post_Type $type): array { |
| 118 | |
| 119 | $supports = ['comments' => false]; |
| 120 | foreach (array_keys($supports) as $key) { |
| 121 | $supports[$key] = $this->wp->postTypeSupports($type->name, $key); |
| 122 | } |
| 123 | |
| 124 | return [ |
| 125 | 'name' => $type->name, |
| 126 | 'label' => $type->label, |
| 127 | 'supports' => $supports, |
| 128 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 129 | 'show_in_rest' => $type->show_in_rest, |
| 130 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 131 | 'rest_base' => $type->rest_base, |
| 132 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 133 | 'rest_namespace' => $type->rest_namespace, |
| 134 | 'public' => $type->public, |
| 135 | ]; |
| 136 | }, |
| 137 | $postTypes)); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @return array<int, array<string, string[]|bool|string>> |
| 142 | */ |
| 143 | private function getTaxonomies(): array { |
| 144 | /** @var \WP_Taxonomy[] $taxonomies */ |
| 145 | $taxonomies = array_filter( |
| 146 | $this->wp->getTaxonomies([], 'objects'), |
| 147 | function($object): bool { |
| 148 | return $object instanceof \WP_Taxonomy; |
| 149 | } |
| 150 | ); |
| 151 | return array_values(array_map( |
| 152 | function(\WP_Taxonomy $taxonomy): array { |
| 153 | return [ |
| 154 | 'name' => $taxonomy->name, |
| 155 | 'label' => $taxonomy->label, |
| 156 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 157 | 'show_in_rest' => $taxonomy->show_in_rest, |
| 158 | 'public' => $taxonomy->public, |
| 159 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 160 | 'rest_base' => $taxonomy->rest_base, |
| 161 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 162 | 'rest_namespace' => $taxonomy->rest_namespace, |
| 163 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 164 | 'object_type' => (array)$taxonomy->object_type, |
| 165 | ]; |
| 166 | }, |
| 167 | $taxonomies |
| 168 | )); |
| 169 | } |
| 170 | } |
| 171 |