| 1 |
<?php |
| 2 |
|
| 3 |
namespace GeminiLabs\SiteReviews\Controllers; |
| 4 |
|
| 5 |
use GeminiLabs\SiteReviews\Contracts\CommandContract; |
| 6 |
use GeminiLabs\SiteReviews\Contracts\ControllerContract; |
| 7 |
use GeminiLabs\SiteReviews\Contracts\PluginContract; |
| 8 |
use GeminiLabs\SiteReviews\HookProxy; |
| 9 |
|
| 10 |
abstract class AbstractController implements ControllerContract |
| 11 |
{ |
| 12 |
use HookProxy; |
| 13 |
|
| 14 |
public function app(): PluginContract |
| 15 |
{ |
| 16 |
return glsr(); |
| 17 |
} |
| 18 |
|
| 19 |
public function download(string $filename, string $content): void |
| 20 |
{ |
| 21 |
if (glsr()->can('edit_others_posts')) { |
| 22 |
nocache_headers(); |
| 23 |
header('Content-Type: text/plain'); |
| 24 |
header("Content-Disposition: attachment; filename=\"{$filename}\""); |
| 25 |
echo wp_specialchars_decode($content); |
| 26 |
glsr_exit(); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
public function execute(CommandContract $command): CommandContract |
| 31 |
{ |
| 32 |
$command->handle(); |
| 33 |
return $command; |
| 34 |
} |
| 35 |
|
| 36 |
protected function getPostId(): int |
| 37 |
{ |
| 38 |
return intval(filter_input(\INPUT_GET, 'post')); |
| 39 |
} |
| 40 |
|
| 41 |
protected function hasQueryPermission(\WP_Query $query, string $postType = ''): bool |
| 42 |
{ |
| 43 |
global $pagenow; |
| 44 |
return glsr()->isAdmin() |
| 45 |
&& $query->is_main_query() |
| 46 |
&& $query->get('post_type') === ($postType ?: glsr()->post_type) |
| 47 |
&& 'edit.php' === $pagenow; |
| 48 |
} |
| 49 |
|
| 50 |
protected function isAdminPage(): bool |
| 51 |
{ |
| 52 |
if (!glsr()->isAdmin()) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
$getPostType = (string) filter_input(\INPUT_GET, 'post_type'); |
| 56 |
return str_starts_with($getPostType, glsr()->post_type) |
| 57 |
|| str_starts_with(get_post_type(), glsr()->post_type); |
| 58 |
} |
| 59 |
|
| 60 |
protected function isAdminScreen(): bool |
| 61 |
{ |
| 62 |
return str_starts_with(glsr_current_screen()->post_type, glsr()->post_type); |
| 63 |
} |
| 64 |
|
| 65 |
protected function isEditor(): bool |
| 66 |
{ |
| 67 |
$screen = glsr_current_screen(); |
| 68 |
return ('post' === $screen->base) |
| 69 |
&& str_starts_with($screen->id, glsr()->post_type) |
| 70 |
&& str_starts_with($screen->post_type, glsr()->post_type); |
| 71 |
} |
| 72 |
|
| 73 |
protected function isListTable(): bool |
| 74 |
{ |
| 75 |
$screen = glsr_current_screen(); |
| 76 |
return 'edit' === $screen->base && $this->isAdminScreen(); |
| 77 |
} |
| 78 |
|
| 79 |
protected function isNoticeAdminScreen(): bool |
| 80 |
{ |
| 81 |
return 'dashboard' === glsr_current_screen()->id || $this->isAdminScreen(); |
| 82 |
} |
| 83 |
|
| 84 |
protected function isReviewAdminPage(): bool |
| 85 |
{ |
| 86 |
return glsr()->isAdmin() && in_array(glsr()->post_type, [ |
| 87 |
filter_input(\INPUT_GET, 'post_type'), |
| 88 |
get_post_type(), |
| 89 |
]); |
| 90 |
} |
| 91 |
|
| 92 |
protected function isReviewEditor(): bool |
| 93 |
{ |
| 94 |
$screen = glsr_current_screen(); |
| 95 |
return ('post' === $screen->base) |
| 96 |
&& glsr()->post_type === $screen->id |
| 97 |
&& glsr()->post_type === $screen->post_type; |
| 98 |
} |
| 99 |
|
| 100 |
protected function isReviewListTable(): bool |
| 101 |
{ |
| 102 |
$screen = glsr_current_screen(); |
| 103 |
return 'edit' === $screen->base |
| 104 |
&& glsr()->post_type === $screen->post_type; |
| 105 |
} |
| 106 |
} |
| 107 |
|