-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: enable post type modules * refactor: post type modules * refactor: post type module admin pages * fix: disable archive modules on hierarchical post types * refactor: apply coding standards * docs: post type modules * refactor: apply coding standards * fix: allow archive modules on hierarchical types
- Loading branch information
Showing
9 changed files
with
280 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Modularity\Helper; | ||
|
||
/** | ||
* Helper class for options | ||
*/ | ||
class Options { | ||
|
||
/** | ||
* Get list of currently available archives slugs that has a template | ||
* @return array | ||
*/ | ||
public static function getSingleTemplateSlugs() | ||
{ | ||
$postTypeNames = self::getPublicPostTypeNames(); | ||
return self::getSingleTemplatesFromPostTypeNames($postTypeNames); | ||
} | ||
|
||
public static function getArchiveTemplateSlugs() | ||
{ | ||
$postTypeNames = self::getPostTypesWithArchives(); | ||
return self::getArchiveTemplatesFromPostTypeNames($postTypeNames); | ||
} | ||
|
||
private static function getSingleTemplatesFromPostTypeNames(array $postTypeNames):array | ||
{ | ||
return self::getTemplatesFromPostTypeNames($postTypeNames, 'single'); | ||
} | ||
|
||
private static function getArchiveTemplatesFromPostTypeNames(array $postTypeNames): array | ||
{ | ||
return self::getTemplatesFromPostTypeNames($postTypeNames, 'archive'); | ||
} | ||
|
||
private function getTemplatesFromPostTypeNames(array $postTypeNames, string $templateType): array | ||
{ | ||
$templates = array_map(function ($postTypeName) use ($templateType) { | ||
$template = \Modularity\Helper\Wp::findCoreTemplates([$templateType . '-' . $postTypeName]); | ||
return ($template) | ||
? $template | ||
: $templateType; | ||
}, $postTypeNames); | ||
|
||
return array_unique($templates); | ||
} | ||
|
||
private static function getPostTypesWithArchives() { | ||
return get_post_types(array( | ||
'has_archive' => true | ||
), 'names'); | ||
} | ||
|
||
private static function getPublicPostTypeNames() { | ||
return get_post_types(array( | ||
'public' => true, | ||
'show_ui' => true, | ||
), 'names'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Modularity\Options; | ||
|
||
/** | ||
* Interface for creating admin pages in WordPress. | ||
*/ | ||
interface AdminPageInterface | ||
{ | ||
/** | ||
* Add hooks for the admin page. | ||
* | ||
* @return void | ||
*/ | ||
public function addHooks(): void; | ||
|
||
/** | ||
* Add the admin page to WordPress. | ||
* | ||
* @return void | ||
*/ | ||
public function addAdminPage(): void; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Modularity\Options; | ||
|
||
use WP_Post_Type; | ||
|
||
/** | ||
* Class ArchivesAdminPage | ||
* Implements the AdminPageInterface for managing archive modules. | ||
*/ | ||
class ArchivesAdminPage implements \Modularity\Options\AdminPageInterface | ||
{ | ||
/** | ||
* @var array The array of enabled post types for archive modules. | ||
*/ | ||
private array $postTypes; | ||
|
||
/** | ||
* ArchivesAdminPage constructor. | ||
* Initializes the class and retrieves enabled post types from options. | ||
*/ | ||
public function __construct() | ||
{ | ||
$options = get_option('modularity-options'); | ||
$this->postTypes = $options['enabled-post-types'] ?? []; | ||
} | ||
|
||
/** | ||
* Add hooks for this admin page. | ||
*/ | ||
public function addHooks(): void | ||
{ | ||
add_action('admin_menu', [$this, 'addAdminPage'], 10); | ||
add_action('after_setup_theme', [$this, 'fixBrokenArchiveLinks'], 10); | ||
} | ||
|
||
/** | ||
* Add the admin page for managing archive modules to the WordPress admin menu. | ||
*/ | ||
public function addAdminPage(): void | ||
{ | ||
foreach ($this->postTypes as $postType) { | ||
$postTypeObject = get_post_type_object($postType); | ||
|
||
if ($this->postTypeAllowsArchiveModules($postTypeObject)) { | ||
$postTypeUrlParam = $postType === 'post' ? '' : '?post_type=' . $postType; | ||
$transcribedPostType = \Modularity\Editor::pageForPostTypeTranscribe('archive-' . $postType); | ||
$editorLink = "options.php?page=modularity-editor&id={$transcribedPostType}"; | ||
add_submenu_page( | ||
'edit.php' . $postTypeUrlParam, | ||
__('Archive modules', 'modularity'), | ||
__('Archive modules', 'modularity'), | ||
'edit_posts', | ||
$editorLink | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Determine if a post type allows archive modules. | ||
* | ||
* @param WP_Post_Type|null $postType The post type to check. | ||
* @return bool Returns true if the post type allows archive modules, false otherwise. | ||
*/ | ||
private function postTypeAllowsArchiveModules(?WP_Post_Type $postType): bool | ||
{ | ||
return !is_null($postType) && $postType->has_archive; | ||
} | ||
|
||
/** | ||
* Fix broken archive links by redirecting to the correct URL. | ||
*/ | ||
public function fixBrokenArchiveLinks(): void | ||
{ | ||
if ( | ||
is_admin() && | ||
isset($_GET['post_type']) && | ||
isset($_GET['page']) && | ||
isset($_GET['id']) && | ||
substr($_GET['page'], 0, 34) == "options.php?page=modularity-editor" | ||
) { | ||
wp_redirect(admin_url($_GET['page'] . "&id=" . $_GET['id']), 302); | ||
exit; | ||
} | ||
} | ||
} |
Oops, something went wrong.