ID; } $belongsTo = MetaTypes::USER; break; case "user-edit.php": $elementId = $_GET['user_id'] ?? null; $belongsTo = MetaTypes::USER; break; case "comment.php": $elementId = $_GET['c'] ?? null; $belongsTo = MetaTypes::COMMENT; break; case "post-new.php": case "post.php": $elementId = $_GET['post'] ?? 'post-new'; $belongsTo = MetaTypes::CUSTOM_POST_TYPE; break; case "edit-tags.php": $elementId = $_GET['taxonomy'] ?? null; $belongsTo = MetaTypes::TAXONOMY; break; case "term.php": $elementId = $_GET['tag_ID'] ?? null; $belongsTo = MetaTypes::TAXONOMY; break; } // validation rules and conditional rendering are not part of ACPT Lite } /** * This method is used by CPT and Comment meta field generators * * @param MetaFieldModel $fieldModel * * @return array */ protected function generateMetaBoxFieldArray(MetaFieldModel $fieldModel): array { $options = []; foreach ($fieldModel->getOptions() as $optionModel) { $options[] = [ 'label' => $optionModel->getLabel(), 'value' => $optionModel->getValue(), ]; } $relations = []; $children = []; $advancedOptions = []; foreach ($fieldModel->getAdvancedOptions() as $advancedOptionModel) { if ($advancedOptionModel->isPlaceholder()) { continue; } $advancedOptions[] = [ 'key' => $advancedOptionModel->getKey(), 'value' => $advancedOptionModel->getValue(), ]; } $validationRules = []; foreach ($fieldModel->getValidationRules() as $validationRuleModel) { $validationRules[] = [ 'condition' => $validationRuleModel->getCondition(), 'value' => $validationRuleModel->getValue(), ]; } $blocks = []; foreach ($fieldModel->getBlocks() as $blockModel) { $nestedFields = []; foreach ($blockModel->getFields() as $nestedFieldModel) { $nestedFields[] = $this->generateMetaBoxFieldArray($nestedFieldModel); } $blocks[] = [ 'id' => $blockModel->getId(), 'name' => $blockModel->getName(), 'label' => $blockModel->getLabel(), 'fields' => $nestedFields ]; } return [ 'id' => $fieldModel->getId(), 'type' => $fieldModel->getType(), 'name' => $fieldModel->getName(), 'defaultValue' => $fieldModel->getDefaultValue(), 'description' => $fieldModel->getDescription(), 'isRequired' => $fieldModel->isRequired(), 'isShowInArchive' => $fieldModel->isShowInArchive(), 'sort' => $fieldModel->getSort(), 'options' => $options, 'relations' => $relations, 'children' => $children, 'advancedOptions' => $advancedOptions, 'validationRules' => $validationRules, 'blocks' => $blocks, ]; } /** * This method is used to generate quick edit fields * * @param string $key * @param string $label * @param mixed $value * @param MetaFieldModel $metaBoxFieldModel * * @return string */ protected static function generateQuickEditField(string $key, string $label, $value, MetaFieldModel $metaBoxFieldModel): string { $return = '
'; $return .= ''; $return .= ''; $return .= ''; $return .= ''; $return .= ''; $return .= ''; $return .= '
'; $return .= ''; $return .= '
'; $return .= '
'; return $return; } /** * @param MetaFieldModel $fieldModel * * @return array */ private static function postTypeList(MetaFieldModel $fieldModel): array { $postQuery = []; if ($fieldModel->getAdvancedOption('filter_post_type')) { $postQuery['post_type'] = $fieldModel->getAdvancedOption('filter_post_type'); } if ($fieldModel->getAdvancedOption('filter_post_status')) { $postQuery['post_status'] = $fieldModel->getAdvancedOption('filter_post_status'); } if ($fieldModel->getAdvancedOption('filter_taxonomy')) { $postQuery['taxonomy'] = $fieldModel->getAdvancedOption('filter_taxonomy'); } return Posts::getList($postQuery); } /** * @param MetaFieldModel $fieldModel * * @return array */ private static function termList(MetaFieldModel $fieldModel): array { $termQuery = []; if ($fieldModel->getAdvancedOption('filter_taxonomy')) { $termQuery['taxonomy'] = $fieldModel->getAdvancedOption('filter_taxonomy'); } return Terms::getList($termQuery); } /** * @param MetaFieldModel $fieldModel * * @return array */ private static function userList(MetaFieldModel $fieldModel): array { $userQuery = []; if ($fieldModel->getAdvancedOption('filter_role')) { $userQuery['role'] = $fieldModel->getAdvancedOption('filter_role'); } return Users::getList($userQuery); } /** * @param string $postTypeName * @return string */ protected function getPostTypeName(string $postTypeName): string { try { return apply_filters("modify_post_type_name", $postTypeName); } catch (\Exception $exception) { do_action("acpt/error", $exception); return $postTypeName; } } /** * @param MetaBoxModel $metaBoxModel * @return string */ protected function metaBoxEditLinkButton(MetaBoxModel $metaBoxModel): string { // check admin permission if (!current_user_can('manage_options')) { return ""; } $url = $this->generateEditMetaBoxLink($metaBoxModel); return "".Translator::translate("Edit meta box").""; } /** * Same as metaBoxEditLinkButton(), but tolerates a null box — the tab-display * generators resolve the "active" box via getBoxById(...) ?? getBoxes()[0], * both of which can be null (no/stale active-tab cookie, or a group with zero * boxes) — render nothing instead of fataling on the non-nullable param above. * * @param MetaBoxModel|null $metaBoxModel * @return string */ protected function metaBoxEditLinkButtonOrEmpty(?MetaBoxModel $metaBoxModel): string { return $metaBoxModel !== null ? $this->metaBoxEditLinkButton($metaBoxModel) : ''; } /** * @param MetaBoxModel $metaBoxModel * @return string */ protected function metaBoxLabel(MetaBoxModel $metaBoxModel): string { return (!empty($metaBoxModel->getLabel())) ? $metaBoxModel->getLabel() : $metaBoxModel->getName(); } /** * Returns a hidden element containing the edit URL for the meta box. * JS will pick this up and inject the edit button into the appropriate header. * * @param MetaBoxModel $metaBoxModel * @return string */ protected function metaBoxEditButtonPlaceholder(MetaBoxModel $metaBoxModel): string { if (!current_user_can('manage_options')) { return ""; } $url = $this->generateEditMetaBoxLink($metaBoxModel); $label = Translator::translate("Edit meta box"); return ""; } /** * @param MetaBoxModel $metaBoxModel * @return string */ protected function metaBoxHeading(MetaBoxModel $metaBoxModel): string { $boxLabel = "
"; $boxLabel .= ""; $boxLabel .= $this->metaBoxLabel($metaBoxModel); $boxLabel .= ""; $boxLabel .= $this->metaBoxEditLinkButton($metaBoxModel); $boxLabel .= "
"; return $boxLabel; } /** * Generates an admin URL link for editing a meta box based on its group association. * * @param MetaBoxModel $metaBoxModel The model representing the meta box. * @return string The generated admin URL link. */ private function generateEditMetaBoxLink(MetaBoxModel $metaBoxModel): string { return admin_url('admin.php?page='.ACPT_LITE_PLUGIN_NAME.'#/edit_meta/'.$metaBoxModel->getGroup()->getId().'#'.$metaBoxModel->getId()); } }