Skip to content

Commit

Permalink
refactor: post type module admin pages
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbrink committed Oct 4, 2023
1 parent 1dc6df0 commit 4076535
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 172 deletions.
5 changes: 3 additions & 2 deletions source/php/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public function __construct()

new Ajax();
new Options\General();
new Options\Archives();

$optionsForSingleViews = new Options\Single();
$archivesAdminPage = new Options\ArchivesAdminPage();
$archivesAdminPage->addHooks();
$optionsForSingleViews = new Options\SingleAdminPage();
$optionsForSingleViews->addHooks();

// Rest Controllers
Expand Down
4 changes: 2 additions & 2 deletions source/php/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ public function getActiveAreas($template)
if (is_array($active) && count($active) === 0
&& !is_numeric($template)
&& strpos($template, 'archive-') !== false
&& !in_array($template, \Modularity\Options\Archives::getArchiveTemplateSlugs())
&& !in_array($template, \Modularity\Options\Single::getSingleTemplateSlugs())) {
&& !in_array($template, \Modularity\Helper\Options::getArchiveTemplateSlugs())
&& !in_array($template, \Modularity\Helper\Options::getSingleTemplateSlugs())) {


$template = explode('-', $template, 2)[0];
Expand Down
60 changes: 60 additions & 0 deletions source/php/Helper/Options.php
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');
}
}
9 changes: 9 additions & 0 deletions source/php/Options/AdminPageInterface.php
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;
}
103 changes: 0 additions & 103 deletions source/php/Options/Archives.php

This file was deleted.

63 changes: 63 additions & 0 deletions source/php/Options/ArchivesAdminPage.php
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;
}
}
}
65 changes: 0 additions & 65 deletions source/php/Options/Single.php

This file was deleted.

38 changes: 38 additions & 0 deletions source/php/Options/SingleAdminPage.php
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
);
}
}
}

0 comments on commit 4076535

Please sign in to comment.