Completed
Push — staging ( 78e662...0f9e2f )
by
unknown
05:16
created

YIKES_Inc_Easy_Mailchimp_Dashboard_Widgets   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 101
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 4
A enqueue_dashboard_widget_script() 0 14 2
A yks_mc_add_dashboard_widget() 0 13 2
A list_stats_dashboard_widget() 0 40 4
1
<?php
2
3
/*
4
* Our ajax is processed inside of class.ajax.php
5
*/
6
7
class YIKES_Inc_Easy_Mailchimp_Dashboard_Widgets {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
9
	// Construction
10
	public function __construct() {
11
12
		if ( apply_filters( 'yikes-mailchimp-dashboard-widgets-enabled', true ) === true &&  yikes_get_mc_api_key() != '' && get_option( 'yikes-mc-api-validation', 'invalid_api_key' ) == 'valid_api_key' ) {
13
14
			// hook in and display our list stats dashboard widget
15
			add_action( 'wp_dashboard_setup', array( $this, 'yks_mc_add_dashboard_widget' ), 10 );
16
17
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_dashboard_widget_script' ) );
18
		}
19
		
20
	}
21
22
	/**
23
	 * Enqueue our JS file on the main dashboard page.
24
	 *
25
	 * @param string $hook
26
	 */
27
	function enqueue_dashboard_widget_script( $hook ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
28
		if ( 'index.php' !== $hook ) {
29
			return;
30
		}
31
32
		wp_register_script( 'yikes-easy-mc-dashboard-widget-script', YIKES_MC_URL . 'admin/js/min/yikes-inc-easy-mailchimp-dashboard-widget.min.js', array( 'jquery' ), 'all', false );
33
		$data_array = array(
34
			'ajax_url'  => esc_url_raw( admin_url( 'admin-ajax.php' ) ),
35
			'preloader' => '<img src="' . esc_url_raw( admin_url( 'images/wpspin_light.gif' ) ) . '" title="' . __( 'Preloader', 'yikes-inc-easy-mailchimp-extender' ) . '" alt="' . __( 'Preloader', 'yikes-inc-easy-mailchimp-extender' ) . '" class="yikes-easy-mc-widget-preloader">',
36
		);
37
		// localize our data, to pass along to JS file
38
		wp_localize_script( 'yikes-easy-mc-dashboard-widget-script', 'yikes_mailchimp_dsahboard_widget', $data_array );
39
		wp_enqueue_script( 'yikes-easy-mc-dashboard-widget-script' );
40
	}
41
42
	/*******************************************************
43
	 * Custom Dashboard Mailchimp Account Activity Widget
44
	 ********************************************************/
45
	/**
46
	 * Add a widget to the dashboard.
47
	 *
48
	 * This function is hooked into the 'wp_dashboard_setup' action below.
49
	 */
50
	function yks_mc_add_dashboard_widget() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
52
		// If the current user is not an admin, abort
53
		if ( current_user_can( apply_filters( 'yikes-mailchimp-admin-widget-capability', apply_filters( 'yikes-mailchimp-user-role-access', 'manage_options' ) ) ) ) {
54
55
			/* List Stats Dashboard Widget */
56
			wp_add_dashboard_widget(
57
				'yikes_easy_mc_list_stats_widget',         // Widget slug.
58
				__( 'Mailchimp List Stats', 'yikes-inc-easy-mailchimp-extender' ),         // Title.
59
				array( $this, 'list_stats_dashboard_widget' ) // Display function.
60
			);
61
		}
62
	}
63
64
	/**
65
	 * Create the function to output our list stats dashboard widget
66
	 */
67
	function list_stats_dashboard_widget() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
68
		// Get our list data!
69
		$list_data = yikes_get_mc_api_manager()->get_list_handler()->get_lists();
70
		
71
		if ( is_wp_error( $list_data ) ) {
72
			$error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging();
73
			$error_logging->maybe_write_to_log( $list_data->get_error_code(), __( "Get Account Lists", 'yikes-inc-easy-mailchimp-extender' ), "Dashboard Activity Widget" );
74
			?>
75
			<p class="yikes-dashboard-widget-section"><?php _e( "There was an error fetching your list data.", 'yikes-inc-easy-mailchimp-extender' ); ?></p>
76
			<?php
77
			return;
78
		}
79
80
		?>
81
		<!-- Dropdown to Change the list -->
82
		<?php if ( ! empty( $list_data ) ) {
83
			?>
84
			<section class="inside-widget yikes-dashboard-widget-section">
85
				<strong class="select-list-title"><?php _e( 'Select a list', 'yikes-inc-easy-mailchimp-extender' ) ?>:</strong>
86
				<select id="yikes-easy-mc-dashboard-change-list" class="widefat">
87
					<?php
88
					foreach ( $list_data as $list ) {
89
						?>
90
						<option value="<?php echo $list['id']; ?>"><?php echo $list['name']; ?></option>
91
						<?php
92
					}
93
					?>
94
				</select>
95
				<p class="description"><?php _e( 'Select a list from the dropdown above. View statistics related to this list below.', 'yikes-inc-easy-mailchimp-extender' ); ?></p>
96
			</section>
97
			<!-- display stats here! -->
98
			<section id="yikes-easy-mc-dashboard-widget-stats">
99
				<?php include_once( YIKES_MC_PATH . 'admin/partials/dashboard-widgets/templates/stats-list-template.php' ); ?>
100
			</section>
101
		<?php } else { ?>
102
			<section id="yikes-easy-mc-dashboard-widget-stats">
103
				<p class="no-lists-error"><?php _e( "Whoops, you don't have any lists set up. Head over to Mailchimp to set up lists.", 'yikes-inc-easy-mailchimp-extender' ); ?></p>
104
			</section>
105
		<?php }
106
	}
107
}
108