Completed
Branch FET/11400/gutenberg-block-mana... (002e7d)
by
unknown
13:10
created

EditorBlockAdminManager   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 96
Duplicated Lines 9.38 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 9
loc 96
rs 10
c 0
b 0
f 0
wmc 19
lcom 2
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init_hook() 0 4 1
B initialize() 0 13 5
A loadCustomPostTypeBlockEditor() 0 6 1
B modifyWpPostTypes() 0 17 6
B coerceEeCptEditorUrlForGutenberg() 0 17 5
A registerAdminScripts() 9 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace EventEspresso\core\services\editor;
4
5
use EE_Register_CPTs;
6
use WP_Post_Type;
7
8
defined('EVENT_ESPRESSO_VERSION') || exit;
9
10
11
12
/**
13
 * Class EditorBlockAdminManager
14
 * Description
15
 *
16
 * @package EventEspresso\core\services\editor
17
 * @author  Brent Christensen
18
 * @since   $VID:$
19
 */
20
class EditorBlockAdminManager extends EditorBlockManager
21
{
22
23
    /**
24
     *  Returns the name of a hookpoint to be used to call initialize()
25
     *
26
     * @return string
27
     */
28
    public function init_hook()
29
    {
30
        return 'AHEE__EE_System__load_CPTs_and_session__complete';
31
    }
32
33
34
    /**
35
     * Perform any early setup required for block editors to functions
36
     *
37
     * @return void
38
     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
39
     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
40
     * @throws \InvalidArgumentException
41
     */
42
    public function initialize()
43
    {
44
        $custom_post_types     = EE_Register_CPTs::get_CPTs();
45
        $espresso_post_types   = array_keys($custom_post_types);
46
        $espresso_post_types[] = 'espresso_registrations';
47
        if (
48
            ($this->action === 'edit' || $this->action === 'create_new' || $this->action === 'edit_attendee')
49
            && in_array($this->page, $espresso_post_types, true)
50
        ) {
51
            $this->loadCustomPostTypeBlockEditor(array_keys($custom_post_types));
52
        }
53
        add_action('admin_url', array($this, 'coerceEeCptEditorUrlForGutenberg'), 10, 3);/**/
54
    }
55
56
57
    public function loadCustomPostTypeBlockEditor(array $custom_post_types)
58
    {
59
        $this->modifyWpPostTypes($custom_post_types);
60
        add_action('admin_enqueue_scripts', array($this, 'registerAdminScripts'), 20);
61
        add_filter('FHEE__EE_Admin_Page_CPT___create_new_cpt_item__replace_editor', 'gutenberg_init', 10, 2);
62
    }
63
64
    /**
65
     * Manipulate globals related to EE Post Type so gutenberg loads.
66
     */
67
    private function modifyWpPostTypes(array $custom_post_types)
68
    {
69
        global $wp_post_types, $_wp_post_type_features;
70
        foreach ($custom_post_types as $post_type) {
71
            $_wp_post_type_features[ $post_type ]['editor'] = true;
72
            if(isset($wp_post_types[ $post_type ]) && $wp_post_types[ $post_type ] instanceof WP_Post_Type){
0 ignored issues
show
Bug introduced by
The class WP_Post_Type does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
73
                $post_type_object = $wp_post_types[ $post_type ];
74
                $post_type_object->show_in_rest = true;
75
                $post_type_object->template = array();
76
                foreach ($this->blocks as $block) {
77
                    if($block->appliesToPostType($post_type)){
78
                        $post_type_object->template[] = $block->getEditorContainer();
79
                    }
80
                }
81
            }
82
        }
83
    }
84
85
86
    public function coerceEeCptEditorUrlForGutenberg($url, $path, $blog_id)
87
    {
88
        if (
89
            $this->page === 'espresso_events'
90
            && ($this->action === 'edit' || $this->action === 'create_new')
91
            && strpos($path, 'post.php') !== false
92
        ) {
93
            return add_query_arg(
94
                array(
95
                    'page'   => $this->page,
96
                    'action' => $this->action
97
                ),
98
                get_site_url($blog_id)
99
            );
100
        }
101
        return $url;
102
    }
103
104
105
106 View Code Duplication
    public function registerAdminScripts()
107
    {
108
        wp_register_script(
109
            'ee-event-editor-blocks',
110
            $this->domain->distributionAssetsUrl() . 'ee-event-editor-blocks.dist.js',
111
            array('wp-blocks'),
112
            filemtime($this->domain->distributionAssetsPath() . 'ee-event-editor-blocks.dist.js')
113
        );
114
    }
115
}
116