-
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.
refactor: post type module admin pages
- Loading branch information
Showing
8 changed files
with
175 additions
and
172 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Modularity\Options; | ||
|
||
interface AdminPageInterface | ||
{ | ||
public function addHooks(): 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,63 @@ | ||
<?php | ||
|
||
namespace Modularity\Options; | ||
|
||
class ArchivesAdminPage implements \Modularity\Options\AdminPageInterface | ||
{ | ||
public function addHooks(): void | ||
{ | ||
add_action('admin_menu', [$this, 'addAdminPage'], 10); | ||
add_action('after_setup_theme', [$this, 'fixBrokenArchiveLinks'], 10); | ||
} | ||
|
||
public function addAdminPage(): void | ||
{ | ||
$options = get_option('modularity-options'); | ||
|
||
if (!isset($options['enabled-post-types']) || !is_array($options['enabled-post-types'])) { | ||
return; | ||
} | ||
|
||
foreach ($options['enabled-post-types'] as $postType) { | ||
$postTypeSlug = $postType; | ||
|
||
// Do not add archive modules for page | ||
if ($postType == 'page') { | ||
continue; | ||
} | ||
|
||
if ($postType == 'post') { | ||
$postType = ''; | ||
} else { | ||
$postType = '?post_type=' . $postType; | ||
} | ||
|
||
/* Checking if the post type has an archive, otherwise bail early. */ | ||
$postTypeObject = get_post_type_object($postTypeSlug); | ||
if (!is_null($postTypeObject) && !$postTypeObject->_builtin && !$postTypeObject->has_archive) { | ||
continue; | ||
} | ||
|
||
$editorLink = 'options.php?page=modularity-editor&id=' . \Modularity\Editor::pageForPostTypeTranscribe('archive-' . $postTypeSlug); | ||
|
||
add_submenu_page( | ||
'edit.php' . $postType, | ||
__('Archive modules', 'modularity'), | ||
__('Archive modules', 'modularity'), | ||
'edit_posts', | ||
$editorLink | ||
); | ||
} | ||
} | ||
|
||
public function fixBrokenArchiveLinks() | ||
{ | ||
if (!is_admin()) { | ||
return; | ||
} | ||
if (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; | ||
} | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace Modularity\Options; | ||
|
||
/** | ||
* Enable modules editor for post type. | ||
*/ | ||
class SingleAdminPage implements \Modularity\Options\AdminPageInterface | ||
{ | ||
|
||
public function addHooks(): void | ||
{ | ||
add_action('admin_menu', [$this, 'addAdminPage'], 10); | ||
} | ||
|
||
public function addAdminPage(): void | ||
{ | ||
$options = get_option('modularity-options'); | ||
|
||
if (!isset($options['enabled-post-types']) || !is_array($options['enabled-post-types'])) { | ||
return; | ||
} | ||
|
||
foreach ($options['enabled-post-types'] as $postType) { | ||
$postTypeSlug = $postType; | ||
$postTypeUrlParam = '?post_type=' . $postType; | ||
$editorLink = 'options.php?page=modularity-editor&id=' . \Modularity\Editor::pageForPostTypeTranscribe('single-' . $postTypeSlug); | ||
|
||
add_submenu_page( | ||
'edit.php' . $postTypeUrlParam, | ||
__('Post type modules', 'modularity'), | ||
__('Post type modules', 'modularity'), | ||
'edit_posts', | ||
$editorLink | ||
); | ||
} | ||
} | ||
} |