| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace UserAccessManager\Controller\Backend; |
| 6 |
|
| 7 |
use UserAccessManager\Object\ObjectHandler; |
| 8 |
use UserAccessManager\UserGroup\UserGroupTypeException; |
| 9 |
use WP_Post; |
| 10 |
|
| 11 |
class PostObjectController extends ObjectController |
| 12 |
{ |
| 13 |
public function addPostColumnsHeader(array $defaults): array |
| 14 |
{ |
| 15 |
$defaults[self::COLUMN_NAME] = TXT_UAM_COLUMN_ACCESS; |
| 16 |
return $defaults; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* @throws UserGroupTypeException |
| 21 |
*/ |
| 22 |
public function addPostColumn(string $columnName, int|string|null $id): void |
| 23 |
{ |
| 24 |
if ($columnName === self::COLUMN_NAME) { |
| 25 |
$post = $this->objectHandler->getPost($id); |
| 26 |
echo $this->getGroupColumn($post->post_type, $post->ID); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* @throws UserGroupTypeException |
| 32 |
*/ |
| 33 |
public function editPostContent(mixed $post): void |
| 34 |
{ |
| 35 |
if ($post instanceof WP_Post) { |
| 36 |
$this->setObjectInformation($post->post_type, $post->ID); |
| 37 |
} |
| 38 |
|
| 39 |
echo $this->getIncludeContents('PostEditForm.php'); |
| 40 |
} |
| 41 |
|
| 42 |
public function addBulkAction(string $columnName): void |
| 43 |
{ |
| 44 |
if ($columnName === self::COLUMN_NAME) { |
| 45 |
$this->getObjectInformation()->setObjectId(null); |
| 46 |
echo $this->getIncludeContents('BulkEditForm.php'); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @throws UserGroupTypeException |
| 52 |
*/ |
| 53 |
public function savePostData(mixed $postParam): void |
| 54 |
{ |
| 55 |
$postId = (is_array($postParam) === true) ? $postParam['ID'] : $postParam; |
| 56 |
$post = $this->objectHandler->getPost($postId); |
| 57 |
$postType = $post->post_type; |
| 58 |
$postId = $post->ID; |
| 59 |
|
| 60 |
if ($postType === 'revision') { |
| 61 |
$postId = $post->post_parent; |
| 62 |
$parentPost = $this->objectHandler->getPost($postId); |
| 63 |
$postType = $parentPost->post_type; |
| 64 |
} |
| 65 |
|
| 66 |
$this->saveObjectData($postType, $postId); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @throws UserGroupTypeException |
| 71 |
*/ |
| 72 |
public function addAttachment(int|string|null $postId): void |
| 73 |
{ |
| 74 |
$post = $this->objectHandler->getPost($postId); |
| 75 |
$postType = $post->post_type; |
| 76 |
$postId = $post->ID; |
| 77 |
$defaultGroups = []; |
| 78 |
|
| 79 |
foreach ($this->userGroupHandler->getFullUserGroups() as $userGroup) { |
| 80 |
if ($userGroup->isDefaultGroupForObjectType($postType) === true) { |
| 81 |
$defaultGroups[$userGroup->getId()] = ['id' => $userGroup->getId()]; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
$this->saveObjectData($postType, $postId, $defaultGroups, true); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @throws UserGroupTypeException |
| 90 |
*/ |
| 91 |
public function saveAttachmentData(array $attachment): array |
| 92 |
{ |
| 93 |
$this->savePostData($attachment['ID']); |
| 94 |
|
| 95 |
return $attachment; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @throws UserGroupTypeException |
| 100 |
*/ |
| 101 |
public function saveAjaxAttachmentData(): void |
| 102 |
{ |
| 103 |
$attachmentId = $this->getRequestParameter('id'); |
| 104 |
$userGroups = $this->getRequestParameter(self::DEFAULT_GROUPS_FORM_NAME); |
| 105 |
|
| 106 |
$this->saveObjectData( |
| 107 |
ObjectHandler::GENERAL_POST_OBJECT_TYPE, |
| 108 |
$attachmentId, |
| 109 |
$userGroups |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
public function removePostData(int|string|null $postId): void |
| 114 |
{ |
| 115 |
$post = $this->objectHandler->getPost($postId); |
| 116 |
$this->removeObjectData($post->post_type, $postId); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @throws UserGroupTypeException |
| 121 |
*/ |
| 122 |
public function showMediaFile(array $formFields, WP_Post $post = null): array |
| 123 |
{ |
| 124 |
if ($this->getRequestParameter('action') !== 'edit') { |
| 125 |
$attachmentId = $this->getRequestParameter('attachment_id'); |
| 126 |
|
| 127 |
if ($attachmentId !== null) { |
| 128 |
$post = $this->objectHandler->getPost($attachmentId); |
| 129 |
} |
| 130 |
|
| 131 |
if ($post instanceof WP_Post) { |
| 132 |
$this->setObjectInformation($post->post_type, $post->ID); |
| 133 |
} |
| 134 |
|
| 135 |
$formFields[self::DEFAULT_GROUPS_FORM_NAME] = [ |
| 136 |
'label' => TXT_UAM_SET_UP_USER_GROUPS, |
| 137 |
'input' => 'editFrom', |
| 138 |
'editFrom' => $this->getIncludeContents('MediaAjaxEditForm.php') |
| 139 |
]; |
| 140 |
} |
| 141 |
|
| 142 |
return $formFields; |
| 143 |
} |
| 144 |
} |
| 145 |
|