AdminGeneralOptionsGet.php
3 months ago
AdminGeneralOptionsPersist.php
3 months ago
CustomFieldKeys.php
3 months ago
EditorMenuFavorites.php
3 months ago
EditorMenuStatus.php
3 months ago
ExtendedValue.php
3 months ago
IntegrationToggle.php
3 months ago
Integrations.php
3 months ago
ListScreenAddColumn.php
3 months ago
ListScreenDelete.php
3 months ago
ListScreenOriginalColumns.php
3 months ago
ListScreenSave.php
3 months ago
ListScreenSelectColumn.php
3 months ago
ListScreenSettings.php
3 months ago
NetworkPostStati.php
3 months ago
NumberFormat.php
3 months ago
RestoreSettingsRequest.php
3 months ago
ScreenOptions.php
3 months ago
NetworkPostStati.php
137 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\RequestHandler\Ajax; |
| 6 | |
| 7 | use AC\Capabilities; |
| 8 | use AC\Nonce; |
| 9 | use AC\Request; |
| 10 | use AC\RequestAjaxHandler; |
| 11 | use AC\Response\Json; |
| 12 | use WP_Site; |
| 13 | |
| 14 | class NetworkPostStati implements RequestAjaxHandler |
| 15 | { |
| 16 | |
| 17 | public function handle(): void |
| 18 | { |
| 19 | if ( ! current_user_can(Capabilities::MANAGE)) { |
| 20 | return; |
| 21 | } |
| 22 | |
| 23 | $request = new Request(); |
| 24 | $response = new Json(); |
| 25 | |
| 26 | if ( ! (new Nonce\Ajax())->verify($request)) { |
| 27 | $response->error(); |
| 28 | } |
| 29 | |
| 30 | $response |
| 31 | ->set_header('Cache-Control', 'max-age=120') |
| 32 | ->set_parameter('options', $this->get_options()) |
| 33 | ->success(); |
| 34 | } |
| 35 | |
| 36 | private function get_distinct_db_values(): array |
| 37 | { |
| 38 | if ( ! function_exists('get_sites')) { |
| 39 | return []; |
| 40 | } |
| 41 | |
| 42 | global $wpdb; |
| 43 | |
| 44 | $queries = []; |
| 45 | foreach (get_sites() as $site) { |
| 46 | /** |
| 47 | * @var WP_Site $site |
| 48 | */ |
| 49 | $table = $wpdb->get_blog_prefix($site->id) . 'posts'; |
| 50 | |
| 51 | $sql = "SELECT DISTINCT `post_status` FROM $table"; |
| 52 | |
| 53 | $queries[] = $sql; |
| 54 | } |
| 55 | |
| 56 | return $wpdb->get_col(implode(" UNION ", $queries)); |
| 57 | } |
| 58 | |
| 59 | private function get_cached_options(): array |
| 60 | { |
| 61 | $values = wp_cache_get('ac-site-settings', 'ac-site-post-status'); |
| 62 | |
| 63 | if ( ! $values) { |
| 64 | $values = $this->get_distinct_db_values(); |
| 65 | |
| 66 | wp_cache_add('ac-site-settings', $values, 'ac-site-post-status', 60); |
| 67 | } |
| 68 | |
| 69 | return $values; |
| 70 | } |
| 71 | |
| 72 | private function get_options(): array |
| 73 | { |
| 74 | global $wp_post_statuses; |
| 75 | |
| 76 | $options = []; |
| 77 | |
| 78 | foreach ($this->get_post_statuses() as $name) { |
| 79 | $status = $wp_post_statuses[$name] ?? null; |
| 80 | |
| 81 | $options[] = [ |
| 82 | 'value' => $name, |
| 83 | 'label' => $status->label ?? $name, |
| 84 | 'group' => $status |
| 85 | ? __('Status', 'codepress-admin-columns') |
| 86 | : __('Other', 'codepress-admin-columns'), |
| 87 | ]; |
| 88 | } |
| 89 | |
| 90 | $defaults = [ |
| 91 | [ |
| 92 | 'value' => '', |
| 93 | 'label' => __('Any post status', 'codepress-admin-columns'), |
| 94 | 'group' => null, |
| 95 | ], |
| 96 | [ |
| 97 | 'value' => 'without_trash', |
| 98 | 'label' => __('Any post status without Trash', 'codepress-admin-columns'), |
| 99 | 'group' => null, |
| 100 | ], |
| 101 | ]; |
| 102 | |
| 103 | uasort($options, function ($a, $b) { |
| 104 | return strnatcasecmp((string)$a['label'], (string)$b['label']); |
| 105 | }); |
| 106 | uasort($options, function ($a, $b) { |
| 107 | return strnatcasecmp((string)$b['group'], (string)$a['group']); |
| 108 | }); |
| 109 | |
| 110 | return array_merge($defaults, $options); |
| 111 | } |
| 112 | |
| 113 | private function get_post_statuses(): array |
| 114 | { |
| 115 | $post_statuses = $this->get_cached_options(); |
| 116 | |
| 117 | if ( ! $post_statuses) { |
| 118 | return []; |
| 119 | } |
| 120 | |
| 121 | $post_statuses[] = 'trash'; |
| 122 | $post_statuses = array_unique(array_merge($post_statuses, array_keys(get_post_statuses()))); |
| 123 | $post_statuses = array_combine($post_statuses, $post_statuses); |
| 124 | |
| 125 | // Exclude 'auto-draft', 'inherit' |
| 126 | $excluded = (array)get_post_stati(['show_in_admin_status_list' => false]); |
| 127 | |
| 128 | foreach ($excluded as $status) { |
| 129 | if (isset($post_statuses[$status])) { |
| 130 | unset($post_statuses[$status]); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return $post_statuses; |
| 135 | } |
| 136 | |
| 137 | } |