Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ojopaul committed Nov 28, 2023
1 parent db0bf10 commit e602749
Show file tree
Hide file tree
Showing 21 changed files with 2,454 additions and 342 deletions.
567 changes: 275 additions & 292 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
defined('ABSPATH') or die('No direct script access allowed.');

if (!defined('PP_AUTHORS_LOADED')) {
define('PP_AUTHORS_VERSION', '4.2.1');
define('PP_AUTHORS_VERSION', '4.2.1.14');
define('PP_AUTHORS_FILE', 'publishpress-authors/publishpress-authors.php');
define('PP_AUTHORS_BASE_PATH', plugin_dir_path(__DIR__ . '/publishpress-authors.php'));
define('PP_AUTHORS_MODULES_PATH', PP_AUTHORS_BASE_PATH . 'src/modules/');
Expand Down
1 change: 1 addition & 0 deletions lib/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"psr-4": {
"MultipleAuthors\\": "../src/core/",
"MultipleAuthorBoxes\\": "../src/modules/author-boxes/classes/",
"MultipleAuthorCategories\\": "../src/modules/author-categories/classes/",
"PPAuthors\\YoastSEO\\": "../src/modules/yoast-seo-integration/src/"
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/vendor/composer/autoload_psr4.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
return array(
'PPAuthors\\YoastSEO\\' => array($baseDir . '/../src/modules/yoast-seo-integration/src'),
'MultipleAuthors\\' => array($baseDir . '/../src/core'),
'MultipleAuthorCategories\\' => array($baseDir . '/../src/modules/author-categories/classes'),
'MultipleAuthorBoxes\\' => array($baseDir . '/../src/modules/author-boxes/classes'),
);
5 changes: 5 additions & 0 deletions lib/vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ComposerStaticInitPPAuthors
'M' =>
array (
'MultipleAuthors\\' => 16,
'MultipleAuthorCategories\\' => 25,
'MultipleAuthorBoxes\\' => 20,
),
);
Expand All @@ -35,6 +36,10 @@ class ComposerStaticInitPPAuthors
array (
0 => __DIR__ . '/../..' . '/../src/core',
),
'MultipleAuthorCategories\\' =>
array (
0 => __DIR__ . '/../..' . '/../src/modules/author-categories/classes',
),
'MultipleAuthorBoxes\\' =>
array (
0 => __DIR__ . '/../..' . '/../src/modules/author-boxes/classes',
Expand Down
12 changes: 6 additions & 6 deletions lib/vendor/composer/installed.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php return array(
'root' => array(
'name' => '__root__',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'a755d706d1b463e9794fde928f9812e369bc7412',
'pretty_version' => '1.0.0+no-version-set',
'version' => '1.0.0.0',
'reference' => NULL,
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'dev' => true,
),
'versions' => array(
'__root__' => array(
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'a755d706d1b463e9794fde928f9812e369bc7412',
'pretty_version' => '1.0.0+no-version-set',
'version' => '1.0.0.0',
'reference' => NULL,
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down
18 changes: 18 additions & 0 deletions src/assets/css/multiple-authors.css
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,24 @@ ul.authors-list li.ui-sortable-helper {
cursor: move;
}

ul.authors-list.no-select {
user-select: none;
}

.author-category-title {
color: #3c434a;
font-size: 14px;
padding: 8px 0;
font-weight: 1.4;
font-weight: 400;
}

ul.authors-list li.sortable-placeholder {
border-style: dashed;
pointer-events: none;
user-select: none;
}

ul.authors-list.authors-current-user-can-assign li:hover {
cursor: move;
}
Expand Down
33 changes: 31 additions & 2 deletions src/assets/js/multiple-authors.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,12 @@ jQuery(document).ready(function ($) {
});
authorsSearch.on("ppma_select2:select", function (e) {
var template = wp.template("authors-author-partial");
$(".authors-list").append(
$(".authors-list:first").append(
window.htmlEnDeCode.htmlDecode(template(e.params.data))
);
authorsSearch.val(null).trigger("change");
handleUsersAuthorField();
handleAuthorCategory();
});
});
}
Expand Down Expand Up @@ -139,6 +140,19 @@ jQuery(document).ready(function ($) {
}
}

function handleAuthorCategory() {
let $authorsCategoryId = '';
let $authorsCategoryTerm = '';
$('.authors-list').each(function () {
$authorsCategoryId = $(this).attr('data-category_id');
$(this).find('.author_categories').each(function () {
$authorsCategoryTerm = $(this).closest('li').find('.author_term').val();
$(this).attr('name', 'author_categories[' + $authorsCategoryTerm + ']');
$(this).val($authorsCategoryId);
});
});
}

function authorsUserSelect2(selector) {
selector.each(function () {
var authorsSearch = $(this).ppma_select2({
Expand Down Expand Up @@ -343,7 +357,22 @@ jQuery(document).ready(function ($) {
});

function sortedAuthorsList(selector) {
selector.sortable().on("click", ".author-remove", function () {
selector.sortable({
connectWith: ".authors-list",
items: "> li:not(.no-drag)",
placeholder: "sortable-placeholder",
update: function (event, ui) {
handleAuthorCategory();
},
receive: function (event, ui) {
$(this).find('.sortable-placeholder').hide();
},
remove: function (event, ui) {
if ($(this).children().length === 1) {
$(this).find('.sortable-placeholder').show();
}
},
}).on("click", ".author-remove", function () {
var el = $(this);
el.closest("li").remove();
handleUsersAuthorField($(this).parent('.authors-list'));
Expand Down
2 changes: 2 additions & 0 deletions src/core/Classes/Legacy/LegacyPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class LegacyPlugin
public $helpers;
public $class_names;
public $shortcode_authors_list;
public $author_categories;

private $options_group = 'multiple_authors_';

Expand Down Expand Up @@ -173,6 +174,7 @@ private function getModulesDirs()
$defaultDirs = [
'modules-settings' => PP_AUTHORS_MODULES_PATH,
'author-boxes' => PP_AUTHORS_MODULES_PATH,
'author-categories' => PP_AUTHORS_MODULES_PATH,
'author-custom-fields' => PP_AUTHORS_MODULES_PATH,
'settings' => PP_AUTHORS_MODULES_PATH,
'multiple-authors' => PP_AUTHORS_MODULES_PATH,
Expand Down
168 changes: 129 additions & 39 deletions src/core/Classes/Post_Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,54 +240,94 @@ public static function render_authors_metabox()
echo self::get_rendered_authors_selection($authors, false); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

/**
* Group author into categories
*
* @param array $author_categories
* @param array $author_relations
* @param array $authors
*
* @return array
*/
public static function group_category_authors($author_categories, $author_relations, $authors) {

// group authors by category slug
$grouped_authors = array_reduce($author_relations, function ($result, $item) {
$result[$item['category_slug']][] = $item;
return $result;
}, []);

// List all authors attached to the post
$remaining_authors = $authors;

$authors_data = [];
foreach ($author_categories as $author_category) {
if (!empty($remaining_authors) && !empty($grouped_authors) && isset($grouped_authors[$author_category['slug']])) {
// get current category term ids
$category_author_ids = array_column($grouped_authors[$author_category['slug']], 'author_term_id');
// get selected authors for the category terms
$selected_authors = array_filter($remaining_authors, function ($author) use ($category_author_ids) {
$term_id = $author->term_id;
return in_array($term_id, $category_author_ids);
});
// update remaining authors
$remaining_authors = array_filter($remaining_authors, function ($author) use ($category_author_ids) {
$term_id = $author->term_id;
return !in_array($term_id, $category_author_ids);
});
} else {
$selected_authors = [];
}

$authors_data[] = [
'title' => $author_category['plural_name'],
'description' => sprintf('Drag and Drop Author to add them to %s category.', $author_category['plural_name']),
'slug' => $author_category['slug'],
'id' => $author_category['id'],
'authors' => $selected_authors
];
}

// Add remaining author to first category
if (!empty($remaining_authors)) {
$authors_data[0]['authors'] = array_merge($authors_data[0]['authors'], $remaining_authors);
}


return $authors_data;
}

/**
* Get rendered authors selection.
*/
public static function get_rendered_authors_selection($authors, $showAvatars = true, $bulkEdit = false)
{
$post = get_post();

$classes = [
'authors-list',
];
if (current_user_can(get_taxonomy('author')->cap->assign_terms)) {
$classes[] = 'authors-current-user-can-assign';
}
?>
<ul class="<?php echo esc_attr(implode(' ', $classes)); ?>">
<?php
if (!empty($authors)) {
foreach ($authors as $author) {
if (!is_object($author) || is_wp_error($author)) {
continue;
}

$display_name = $author->display_name;
$term = is_a($author, 'WP_User') ? 'u' . $author->ID : $author->term_id;

$isGuest = 0;
if (is_a($author, Author::class)) {
$isGuest = $author->is_guest() ? 1 : 0;
}

$args = [
'display_name' => $display_name,
'term' => $term,
'is_guest' => $isGuest,
];

if ($showAvatars) {
$args['avatar'] = $author->get_avatar(20);
}

echo self::get_rendered_author_partial($args); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
?>
</ul>
<?php
wp_nonce_field('authors-save', 'authors-save');

if (current_user_can(get_taxonomy('author')->cap->assign_terms)) {
?>
$author_categories = \MA_Author_Categories::get_author_categories(['category_status' => 1]);

if (!empty($author_categories)) {
$author_relations = \MA_Author_Categories::get_author_relations(['post_id' => $post->ID]);
$author_categories_data = self::group_category_authors($author_categories, $author_relations, $authors);
} else {
$author_categories_data = [];
$author_categories_data[] = [
'title' => '',
'description' => '',
'slug' => '',
'id' => '',
'authors' => $authors
];
}
?>
<?php if (current_user_can(get_taxonomy('author')->cap->assign_terms)) : ?>
<select data-nonce="<?php
echo esc_attr(wp_create_nonce('authors-search')); ?>"
id="publishpress-authors-author-select"
Expand All @@ -303,12 +343,59 @@ class="authors-select2 authors-search"
'display_name' => '{{ data.display_name }}',
'term' => '{{ data.id }}',
'is_guest' => '{{ data.is_guest }}',
'category_id' => 0,
]
);
?>
</script>
<p class="description"> </p>
<?php endif; ?>
<?php foreach ($author_categories_data as $author_category_data) : ?>
<?php if (!empty($author_category_data['title'])) : ?>
<div class="author-category-title"><?php echo esc_html($author_category_data['title']); ?></div>
<?php endif; ?>
<ul class="<?php echo esc_attr(implode(' ', $classes)); ?>" data-category_id="<?php echo esc_attr($author_category_data['id']); ?>">
<?php if (!empty($author_category_data['description'])) : ?>
<li class="sortable-placeholder no-drag" style="<?php echo (!empty($author_category_data['authors']) ? 'display: none' : ''); ?>"><p class="description"><?php echo esc_html($author_category_data['description']); ?></p></li>
<?php endif; ?>
<?php
if (!empty($author_category_data['authors'])) {
foreach ($author_category_data['authors'] as $author) {
if (!is_object($author) || is_wp_error($author)) {
continue;
}

$display_name = $author->display_name;
$term = is_a($author, 'WP_User') ? 'u' . $author->ID : $author->term_id;

$isGuest = 0;
if (is_a($author, Author::class)) {
$isGuest = $author->is_guest() ? 1 : 0;
}

$args = [
'display_name' => $display_name,
'term' => $term,
'is_guest' => $isGuest,
'category_id' => $author_category_data['id'],
];

if ($showAvatars) {
$args['avatar'] = $author->get_avatar(20);
}

echo self::get_rendered_author_partial($args); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
?>
</ul>
<?php endforeach; ?>
<?php
wp_nonce_field('authors-save', 'authors-save');

if (current_user_can(get_taxonomy('author')->cap->assign_terms)) {
?>
<?php
$post = get_post();
$userAuthor = get_user_by('ID', $post->post_author);
$postAuthorId = $post->post_author;

Expand Down Expand Up @@ -416,6 +503,7 @@ private static function get_rendered_author_partial($args = [])
'avatar' => '',
'term' => '',
'is_guest' => 0,
'category_id' => 0,
];

$args = array_merge($defaults, $args);
Expand All @@ -435,7 +523,8 @@ private static function get_rendered_author_partial($args = [])
<?php
endif; ?>
<span class="display-name"><?php echo esc_html($args['display_name']); ?></span>
<input type="hidden" name="authors[]" value="<?php echo esc_attr($args['term']); ?>">
<input type="hidden" name="authors[]" value="<?php echo esc_attr($args['term']); ?>" class="author_term">
<input type="hidden" name="author_categories[<?php echo esc_attr($args['term']); ?>]" class="author_categories" value="<?php echo esc_attr($args['category_id']); ?>">
</li>
<?php
return ob_get_clean();
Expand Down Expand Up @@ -504,12 +593,13 @@ public static function action_save_post_authors_metabox($post_id, $post)
}

$authors = isset($_POST['authors']) ? Utils::sanitizeArray($_POST['authors']) : []; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$author_categories = isset($_POST['author_categories']) ? Utils::sanitizeArray($_POST['author_categories']) : []; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$authors = self::remove_dirty_authors_from_authors_arr($authors);

$fallbackUserId = isset($_POST['fallback_author_user']) ? (int)$_POST['fallback_author_user'] : null;
$disableAuthorBox = isset($_POST['ppma_disable_author_box']) ? (int)$_POST['ppma_disable_author_box'] : 0;

Utils::set_post_authors($post_id, $authors, true, $fallbackUserId);
Utils::set_post_authors($post_id, $authors, true, $fallbackUserId, $author_categories);
if (isset($_POST['ppma_save_disable_author_box']) && (int)$_POST['ppma_save_disable_author_box'] > 0) {
update_post_meta($post_id, 'ppma_disable_author_box', $disableAuthorBox);
}
Expand Down
Loading

0 comments on commit e602749

Please sign in to comment.