Skip to content

Commit 459133a

Browse files
authored
[RUA-56] code revamp - step 1 (#56)
* [RUA-56] container, providers, scaffolding * [RUA-56] level repo * [RUA-56] move membership automators to new architecture * [RUA-56] move post type to new architecture * [RUA-56] move query filters * [RUA-56] move content mode * [RUA-56] move rest api protection * [RUA-56] boot new code alongside legacy * [RUA-56] move shortcodes. modules. settingrepo * [RUA-56] const * [RUA-56] autoload check * [RUA-56] update helper * [RUA-56] cs fixer * [RUA-56] backwards compat * [RUA-56] fix membership extension ui
1 parent ffbaa02 commit 459133a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+4330
-823
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ node_modules/
44
.idea/
55
*.cache
66
pnpm-lock.yaml
7+
8+
/vendor/

.php-cs-fixer.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
return (new PhpCsFixer\Config())
4+
->setRules([
5+
'@PSR12' => true,
6+
'array_indentation' => true,
7+
'blank_line_after_opening_tag' => true,
8+
'combine_consecutive_issets' => true,
9+
'combine_consecutive_unsets' => true,
10+
'class_attributes_separation' => ['elements' => ['method' => 'one']],
11+
'single_quote' => true,
12+
'binary_operator_spaces' => [
13+
'default' => 'single_space',
14+
'operators' => [
15+
'=>' => 'align_single_space_minimal'
16+
]
17+
],
18+
'braces' => [
19+
'allow_single_line_closure' => true
20+
],
21+
'concat_space' => true,
22+
'include' => true,
23+
'lowercase_cast' => true,
24+
'no_extra_blank_lines' => [
25+
'tokens' => [
26+
'curly_brace_block',
27+
'extra',
28+
'parenthesis_brace_block',
29+
'square_brace_block',
30+
'throw',
31+
'use'
32+
]
33+
],
34+
'no_multiline_whitespace_around_double_arrow' => true,
35+
'no_whitespace_in_blank_line' => true,
36+
'object_operator_without_whitespace' => true,
37+
'ternary_operator_spaces' => true,
38+
'trim_array_spaces' => true,
39+
'lowercase_static_reference' => true,
40+
'no_superfluous_elseif' => true,
41+
'no_useless_else' => true,
42+
'no_useless_return' => true,
43+
'trailing_comma_in_multiline' => false
44+
])
45+
->setLineEnding(PHP_EOL);

admin/level-edit.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ public function ajax_extend_membership()
166166
*/
167167
public function create_meta_boxes($post)
168168
{
169-
RUA_App::instance()->level_manager->populate_metadata();
170-
171169
$path = plugin_dir_path(__FILE__) . '../view/';
172170

173171
$boxes = [];

admin/level-list-table.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public function __construct($args = [])
4242
'screen' => isset($args['screen']) ? $args['screen'] : null
4343
]);
4444
$this->restrict_post_type = get_post_type_object(RUA_App::TYPE_RESTRICT);
45-
$this->automators = RUA_App::instance()->get_level_automators();
45+
$this->automators = rua()->get(\RestrictUserAccess\Membership\Automator\AutomatorService::class)
46+
->get_level_automators();
4647
}
4748

4849
/**
@@ -154,9 +155,6 @@ public function prepare_items()
154155
'total_pages' => ceil($total_items / $per_page),
155156
'per_page' => $per_page
156157
]);
157-
158-
//Make sure filter is run
159-
RUA_App::instance()->level_manager->populate_metadata();
160158
}
161159

162160
/**
@@ -507,9 +505,9 @@ public function column_role($post)
507505
continue;
508506
}
509507

510-
/** @var RUA_Member_Automator $automator */
508+
/** @var \RestrictUserAccess\Membership\Automator\AbstractAutomator $automator */
511509
$automator = $this->automators->get($automatorData['name']);
512-
if ($automator->get_type() !== RUA_Member_Automator::TYPE_TRAIT) {
510+
if ($automator->get_type() !== 'trait') {
513511
continue;
514512
}
515513

app.php

Lines changed: 7 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
defined('ABSPATH') || exit;
1010

11+
/**
12+
* @deprecated
13+
*/
1114
final class RUA_App
1215
{
1316
/**
@@ -24,7 +27,7 @@ final class RUA_App
2427
/**
2528
* Post Type for restriction
2629
*/
27-
const TYPE_RESTRICT = 'restriction';
30+
const TYPE_RESTRICT = \RestrictUserAccess\Level\PostType::NAME;
2831

2932
/**
3033
* Post type statuses
@@ -73,9 +76,6 @@ final class RUA_App
7376
*/
7477
public $level_manager;
7578

76-
/** @var RUA_Member_Automator[]|RUA_Collection<RUA_Member_Automator> */
77-
private $level_automators;
78-
7979
public function __construct()
8080
{
8181
$this->level_manager = new RUA_Level_Manager();
@@ -114,10 +114,6 @@ public function __construct()
114114
'edit_user_profile_update',
115115
[$this,'save_user_profile']
116116
);
117-
add_action(
118-
'delete_post',
119-
[$this,'sync_level_deletion']
120-
);
121117
add_action(
122118
'delete_user',
123119
[$this, 'sync_user_deletion'],
@@ -149,11 +145,8 @@ public function __construct()
149145
);
150146
} else {
151147
new RUA_Admin_Bar();
152-
new RUA_Content_Mode();
153148
}
154149

155-
new RUA_Query_Filters();
156-
157150
add_action('wpca/loaded', [$this, 'ensure_wpca_loaded']);
158151

159152
add_shortcode(
@@ -165,17 +158,10 @@ public function __construct()
165158
'cas/user_visibility',
166159
[$this,'sidebars_check_levels']
167160
);
168-
169-
add_filter(
170-
'rest_authentication_errors',
171-
[$this, 'rest_api_access']
172-
);
173161
}
174162

175163
public function ensure_wpca_loaded()
176164
{
177-
$this->process_level_automators();
178-
179165
//hook early, other plugins might add dynamic caps later
180166
//fixes problem with WooCommerce Orders
181167
//todo: verify if this is still an issue, now that we run in wpca/loaded
@@ -518,45 +504,6 @@ public function get_levels()
518504
return $this->levels;
519505
}
520506

521-
/**
522-
* Delete foreign metadata belonging to level
523-
*
524-
* @since 0.11.1
525-
* @param int $post_id
526-
* @return void
527-
*/
528-
public function sync_level_deletion($post_id)
529-
{
530-
$post = get_post($post_id);
531-
532-
if (!$post || $post->post_type != RUA_App::TYPE_RESTRICT) {
533-
return;
534-
}
535-
536-
global $wpdb;
537-
538-
//Delete user levels
539-
$wpdb->query($wpdb->prepare(
540-
"DELETE FROM $wpdb->usermeta
541-
WHERE
542-
(meta_key = %s AND meta_value = %d)
543-
OR
544-
meta_key = %s",
545-
self::META_PREFIX . 'level',
546-
$post_id,
547-
self::META_PREFIX . 'level_' . $post_id
548-
));
549-
550-
//Delete nav menu item levels
551-
$wpdb->query($wpdb->prepare(
552-
"DELETE FROM $wpdb->postmeta
553-
WHERE
554-
meta_key = %s AND meta_value = %d",
555-
'_menu_item_level',
556-
$post_id
557-
));
558-
}
559-
560507
public function sync_user_deletion($id, $reassign, $user)
561508
{
562509
global $wpdb;
@@ -634,142 +581,11 @@ public function load_admin_scripts($hook)
634581
}
635582

636583
/**
637-
* @return RUA_Collection|RUA_Member_Automator[]
584+
* @return RUA_Collection|\RestrictUserAccess\Membership\Automator\AbstractAutomator[]
638585
*/
639586
public function get_level_automators()
640587
{
641-
if ($this->level_automators === null) {
642-
$automators = [
643-
new RUA_Role_Member_Automator(),
644-
new RUA_Role_Sync_Member_Automator(),
645-
new RUA_LoggedIn_Member_Automator(),
646-
new RUA_BP_Member_Type_Member_Automator(),
647-
new RUA_EDD_Product_Member_Automator(),
648-
new RUA_WooProduct_Member_Automator(),
649-
new RUA_GiveWP_Donation_Member_Automator()
650-
];
651-
652-
$this->level_automators = new RUA_Collection();
653-
/** @var RUA_Member_Automator $automator */
654-
foreach ($automators as $automator) {
655-
if ($automator->can_enable()) {
656-
$this->level_automators->put($automator->get_name(), $automator);
657-
if (is_admin()) {
658-
add_action(
659-
'wp_ajax_rua/automator/' . $automator->get_name(),
660-
[$automator,'ajax_print_content']
661-
);
662-
}
663-
}
664-
}
665-
}
666-
return $this->level_automators;
667-
}
668-
669-
public function process_level_automators()
670-
{
671-
$metadata = $this->level_manager->metadata();
672-
$levels = $this->get_levels();
673-
$automators = $this->get_level_automators();
674-
675-
foreach ($levels as $level) {
676-
if ($level->post_status != RUA_App::STATUS_ACTIVE) {
677-
continue;
678-
}
679-
680-
$automators_data = $metadata->get('member_automations')->get_data($level->ID);
681-
if (empty($automators_data)) {
682-
continue;
683-
}
684-
685-
foreach ($automators_data as $automator_data) {
686-
if (!isset($automator_data['value'],$automator_data['name'])) {
687-
continue;
688-
}
689-
690-
if (!$automators->has($automator_data['name'])) {
691-
continue;
692-
}
693-
694-
$automators->get($automator_data['name'])->queue($level->ID, $automator_data['value']);
695-
}
696-
}
697-
698-
foreach ($automators as $automator) {
699-
if (!empty($automator->get_level_data())) {
700-
$automator->add_callback();
701-
}
702-
}
703-
}
704-
705-
public function rest_api_access($result)
706-
{
707-
//bail if auth has been handled elsewhere
708-
if ($result === true || is_wp_error($result)) {
709-
return $result;
710-
}
711-
712-
if (rua_get_user()->has_global_access()) {
713-
return $result;
714-
}
715-
716-
if (!get_option('rua_rest_api_access', 1)) {
717-
return $result;
718-
}
719-
720-
//Contributor is the lowest role that should have access,
721-
//since they can see content in admin area
722-
if (current_user_can('edit_posts')) {
723-
return $result;
724-
}
725-
726-
$restricted = [
727-
'/wp/v2/search' => true,
728-
'/wp/v2/users' => true
729-
];
730-
731-
$ignored_post_types = [
732-
'nav_menu_item' => true,
733-
'wp_block' => true,
734-
'wp_template' => true,
735-
'wp_template_part' => true,
736-
'wp_navigation' => true
737-
];
738-
foreach (get_post_types(['show_in_rest' => true], 'objects') as $post_type) {
739-
if (empty($post_type->rest_base)) {
740-
continue;
741-
}
742-
if (isset($ignored_post_types[$post_type->name])) {
743-
continue;
744-
}
745-
$restricted['/' . $post_type->rest_namespace . '/' . $post_type->rest_base] = true;
746-
}
747-
$ignored_taxonomies = [
748-
'menu' => true,
749-
];
750-
foreach (get_taxonomies(['show_in_rest' => true], 'objects') as $taxonomy) {
751-
if (empty($taxonomy->rest_base)) {
752-
continue;
753-
}
754-
if (isset($ignored_taxonomies[$post_type->name])) {
755-
continue;
756-
}
757-
$restricted['/' . $taxonomy->rest_namespace . '/' . $taxonomy->rest_base] = true;
758-
}
759-
760-
global $wp;
761-
762-
$route = $wp->query_vars['rest_route'];
763-
$route = preg_replace('/(\/\d+)$/', '', $route, 1);
764-
765-
if (!isset($restricted[$route])) {
766-
return $result;
767-
}
768-
769-
return new WP_Error(
770-
'rest_forbidden',
771-
__('Sorry, you are not allowed to do that.'),
772-
['status' => rest_authorization_required_code()]
773-
);
588+
return rua()->get(\RestrictUserAccess\Membership\Automator\AutomatorService::class)
589+
->get_level_automators();
774590
}
775591
}

assets/js/edit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
$date.val(expiration);
4848
$('.js-rua-extend-type-1').prop('checked', true);
4949
} else {
50-
$date.val('');
50+
$date.val($date.prop('min'));
5151
$('.js-rua-extend-type-0').prop('checked', true);
5252
}
5353

0 commit comments

Comments
 (0)