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