Skip to content

Commit

Permalink
feat: post type modules (#500)
Browse files Browse the repository at this point in the history
* 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
thorbrink authored Oct 5, 2023
1 parent 60669e1 commit 9cbcce2
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 110 deletions.
6 changes: 5 additions & 1 deletion source/php/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public function __construct()
new Upgrade();
new Ajax();
new Options\General();
new Options\Archives();

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

// Rest Controllers
$modulesRestController = new Api\V1\Modules();
Expand Down
29 changes: 27 additions & 2 deletions source/php/Display.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Throwable;
use ComponentLibrary\Init as ComponentLibraryInit;
use WP_Post;

class Display
{
Expand Down Expand Up @@ -206,15 +207,39 @@ public function init()
$this->modules = \Modularity\Editor::getPostModules($archiveSlug);
$this->options = get_option('modularity_' . $archiveSlug . '_sidebar-options');
} else {
$this->modules = \Modularity\Editor::getPostModules($post->ID);
$this->options = get_post_meta($realPostID, 'modularity-sidebar-options', true);
$this->setupModulesForSingle($post, $realPostID);
}
add_action('dynamic_sidebar_before', array($this, 'outputBefore'));
add_action('dynamic_sidebar_after', array($this, 'outputAfter'));

add_filter('sidebars_widgets', array($this, 'hideWidgets'));
}

private function setupModulesForSingle(WP_Post $post, int $realPostID) {
$singleSlug = \Modularity\Helper\Wp::getSingleSlug();
$this->modules = \Modularity\Editor::getPostModules($post->ID);
$this->options = get_post_meta($realPostID, 'modularity-sidebar-options', true);

if ($singleSlug) {
$this->options = !is_array($this->options) ? [] : $this->options;
$this->modules = !is_array($this->modules) ? [] : $this->modules;

$this->options = array_merge($this->options, get_option('modularity_' . $singleSlug . '_sidebar-options'));
$this->modules = $this->mergeModules($this->modules, \Modularity\Editor::getPostModules($singleSlug));
}
}

private function mergeModules($first, $second): array
{
foreach ($first as $sidebar => $modulesInSidebar) {
if (isset($second[$sidebar]['modules'])) {
$second[$sidebar]['modules'] = array_merge($second[$sidebar]['modules'], $modulesInSidebar['modules']);
}
}

return $second;
}

/**
* Unsets (hides) widgets from sidebar if set in Modularity options
* @param array $sidebars Sidebars and widgets
Expand Down
13 changes: 9 additions & 4 deletions source/php/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +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\Helper\Options::getArchiveTemplateSlugs())
&& !in_array($template, \Modularity\Helper\Options::getSingleTemplateSlugs())) {


$template = explode('-', $template, 2)[0];
Expand Down Expand Up @@ -388,8 +389,12 @@ public static function getPostModules($postId)
// Cached results
static $cachedResults;

if(isset($cachedResults)) {
return $cachedResults;
if(isset($cachedResults) && isset($cachedResults[$postId])) {
return $cachedResults[$postId];
}

if( !is_array($cachedResults) ) {
$cachedResults = array();
}

//Declarations
Expand Down Expand Up @@ -482,7 +487,7 @@ public static function getPostModules($postId)
}

// Cache results to reuse in the same instance
$cachedResults = $retModules;
$cachedResults[$postId] = $retModules;

return $retModules;
}
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');
}
}
17 changes: 17 additions & 0 deletions source/php/Helper/Wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,23 @@ public static function getArchiveSlug()

return false;
}

public static function getSingleSlug()
{
global $wp_query;

if ($wp_query && is_single()) {
$postType = get_post_type();

if (isset($wp_query->query_vars['post_type']) && !empty($wp_query->query_vars['post_type'])) {
$postType = $wp_query->query_vars['post_type'];
}

return 'single-' . get_post_type_object($postType)->name;
}

return false;
}

public static function deprecatedFunction($message)
{
Expand Down
23 changes: 23 additions & 0 deletions source/php/Options/AdminPageInterface.php
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;
}
103 changes: 0 additions & 103 deletions source/php/Options/Archives.php

This file was deleted.

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

0 comments on commit 9cbcce2

Please sign in to comment.