Completed
Push — develop ( 370152...d94e50 )
by David
03:11
created

Wordlift_Admin::enqueue_scripts()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 56
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 22
nc 4
nop 0
dl 0
loc 56
rs 9.0544
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * The admin-specific functionality of the plugin.
5
 *
6
 * @link       https://wordlift.io
7
 * @since      1.0.0
8
 *
9
 * @package    Wordlift
10
 * @subpackage Wordlift/admin
11
 */
12
13
/**
14
 * The admin-specific functionality of the plugin.
15
 *
16
 * Defines the plugin name, version, and two examples hooks for how to
17
 * enqueue the admin-specific stylesheet and JavaScript.
18
 *
19
 * @package    Wordlift
20
 * @subpackage Wordlift/admin
21
 * @author     WordLift <[email protected]>
22
 */
23
class Wordlift_Admin {
24
25
	/**
26
	 * The ID of this plugin.
27
	 *
28
	 * @since    1.0.0
29
	 * @access   private
30
	 * @var      string $plugin_name The ID of this plugin.
31
	 */
32
	private $plugin_name;
33
34
	/**
35
	 * The version of this plugin.
36
	 *
37
	 * @since    1.0.0
38
	 * @access   private
39
	 * @var      string $version The current version of this plugin.
40
	 */
41
	private $version;
42
43
	/**
44
	 * Initialize the class and set its properties.
45
	 *
46
	 * @since    1.0.0
47
	 *
48
	 * @param   string                           $plugin_name           The name of this plugin.
49
	 * @param   string                           $version               The version of this plugin.
50
	 * @param    \Wordlift_Configuration_Service $configuration_service The configuration service.
51
	 * @param    \Wordlift_Notice_Service        $notice_service        The notice service.
52
	 */
53
	public function __construct( $plugin_name, $version, $configuration_service, $notice_service ) {
54
55
		$this->plugin_name = $plugin_name;
56
		$this->version     = $version;
57
58
		$dataset_uri = $configuration_service->get_dataset_uri();
59
		$key         = $configuration_service->get_key();
60
61
		if ( empty( $dataset_uri ) ) {
62
			if ( empty( $key ) ) {
63
				$error = __( 'WordLift\'s key is unset: WordLift requires a key.', 'wordlift' );
64
			} else {
65
				$error = __( 'WordLift\'s dataset URI is unset: please retry WordLift\'s configuration.', 'wordlift' );
66
			}
67
			$notice_service->add_error( $error );
68
		}
69
70
	}
71
72
	/**
73
	 * Register the stylesheets for the admin area.
74
	 *
75
	 * @since    1.0.0
76
	 */
77
	public function enqueue_styles() {
78
79
		/**
80
		 * This function is provided for demonstration purposes only.
81
		 *
82
		 * An instance of this class should be passed to the run() function
83
		 * defined in Wordlift_Loader as all of the hooks are defined
84
		 * in that particular class.
85
		 *
86
		 * The Wordlift_Loader will then create the relationship
87
		 * between the defined hooks and the functions defined in this
88
		 * class.
89
		 */
90
91
		wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-admin.css', array(), $this->version, 'all' );
92
93
	}
94
95
	/**
96
	 * Register the JavaScript for the admin area.
97
	 *
98
	 * @since    1.0.0
99
	 */
100
	public function enqueue_scripts() {
101
102
		/**
103
		 * This function is provided for demonstration purposes only.
104
		 *
105
		 * An instance of this class should be passed to the run() function
106
		 * defined in Wordlift_Loader as all of the hooks are defined
107
		 * in that particular class.
108
		 *
109
		 * The Wordlift_Loader will then create the relationship
110
		 * between the defined hooks and the functions defined in this
111
		 * class.
112
		 */
113
114
		$configuration_service = Wordlift_Configuration_Service::get_instance();
115
116
		// Enqueue the admin scripts.
117
		wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/wordlift-admin.bundle.js', array(
118
			'jquery',
119
			'underscore',
120
			'backbone',
121
		), $this->version, false );
122
123
		// Set the basic params.
124
		$params = array(
125
			// @todo scripts in admin should use wp.post.
126
			'ajax_url'        => admin_url( 'admin-ajax.php' ),
127
			// @todo remove specific actions from settings.
128
			'action'          => 'entity_by_title',
129
			'datasetUri'      => $configuration_service->get_dataset_uri(),
130
			'language'        => $configuration_service->get_language_code(),
131
			'link_by_default' => $configuration_service->is_link_by_default(),
132
			'l10n'            => array(
133
				'You already published an entity with the same name' => __( 'You already published an entity with the same name: ', 'wordlift' ),
134
				'logo_selection_title'                               => __( 'WordLift Choose Logo', 'wordlift' ),
135
				'logo_selection_button'                              => array( 'text' => __( 'Choose Logo', 'wordlift' ) ),
136
			),
137
		);
138
139
		// Set post-related values if there's a current post.
140
		if ( null !== $post = $entity_being_edited = get_post() ) {
141
142
			$params['post_id']           = $entity_being_edited->ID;
143
			$params['entityBeingEdited'] = isset( $entity_being_edited->post_type ) && Wordlift_Entity_Service::TYPE_NAME == $entity_being_edited->post_type && is_numeric( get_the_ID() );
144
			// We add the `itemId` here to give a chance to the analysis to use it in order to tell WLS to exclude it
145
			// from the results, since we don't want the current entity to be discovered by the analysis.
146
			//
147
			// See https://github.com/insideout10/wordlift-plugin/issues/345
148
			$params['itemId'] = Wordlift_Entity_Service::get_instance()->get_uri( $entity_being_edited->ID );
149
150
		}
151
152
		// Finally output the params as `wlSettings` for JavaScript code.
153
		wp_localize_script( $this->plugin_name, 'wlSettings', $params );
154
155
	}
156
157
}
158