Installation::deactivate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Installation
4
 *
5
 * @package SimpleCalendar
6
 */
7
namespace SimpleCalendar;
8
9
use SimpleCalendar\Admin\Pages;
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * Installation.
17
 *
18
 * Static class that deals with plugin activation and deactivation events.
19
 *
20
 * @since 3.0.0
21
 */
22
class Installation {
23
24
	/**
25
	 * What happens when the plugin is activated.
26
	 *
27
	 * @since 3.0.0
28
	 */
29
	public static function activate() {
30
31
		include_once 'functions/shared.php';
32
		include_once 'functions/admin.php';
33
34
		self::create_terms();
35
		self::create_options();
36
37
		do_action( 'simcal_activated' );
38
	}
39
40
	/**
41
	 * What happens when the plugin is deactivated.
42
	 *
43
	 * @since 3.0.0
44
	 */
45
	public static function deactivate() {
46
47
		flush_rewrite_rules();
48
49
		do_action( 'simcal_deactivated' );
50
	}
51
52
	/**
53
	 * Create default terms.
54
	 *
55
	 * @since 3.0.0
56
	 */
57
	public static function create_terms() {
58
59
		$taxonomies = array(
60
			'calendar_feed' => array(
61
				'google',
62
				'grouped-calendar',
63
			),
64
			'calendar_type' => array(
65
				'default-calendar'
66
			)
67
		);
68
69
		foreach ( $taxonomies as $taxonomy => $terms ) {
70
			foreach ( $terms as $term ) {
71
				if ( ! get_term_by( 'slug', sanitize_title( $term ), $taxonomy ) ) {
72
					wp_insert_term( $term, $taxonomy );
73
				}
74
			}
75
		}
76
77
	}
78
79
	/**
80
	 * Sets the default options.
81
	 *
82
	 * @since 3.0.0
83
	 */
84
	public static function create_options() {
85
86
		$default = '';
87
		$page    = 'settings';
88
		$settings_pages  = new Pages( $page );
89
		$plugin_settings = $settings_pages->get_settings();
90
91
		if ( $plugin_settings && is_array( $plugin_settings ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $plugin_settings of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
92
93
			foreach ( $plugin_settings as $id => $settings ) {
94
95
				$group = 'simple-calendar_' . $page . '_' . $id;
96
97
				if ( isset( $settings['sections'] ) ) {
98
99
					if ( $settings['sections'] && is_array( $settings['sections'] ) ) {
100
101
						foreach ( $settings['sections'] as $section_id => $section ) {
102
103
							if ( isset( $section['fields'] ) ) {
104
105
								if ( $section['fields'] && is_array( $section['fields'] ) ) {
106
107
									foreach ( $section['fields'] as $key => $field ) {
108
109
										if ( isset ( $field['type'] ) ) {
110
											// Maybe an associative array.
111
											if ( is_int( $key ) ) {
112
												$default[ $section_id ] = self::get_field_default_value( $field );
113
											} else {
114
												$default[ $section_id ][ $key ] = self::get_field_default_value( $field );
115
											}
116
										}
117
118
									} // Loop fields.
119
120
								} // Are fields non empty?
121
122
							} // Are there fields?
123
124
						} // Loop fields sections.
125
126
					} // Are sections non empty?
127
128
				} // Are there sections?
129
130
				add_option( $group, $default, '', true );
131
132
				// Reset before looping next settings page.
133
				$default = '';
134
			}
135
136
		}
137
	}
138
139
	/**
140
	 * Get field default value.
141
	 *
142
	 * Helper function to set the default value of a field.
143
	 *
144
	 * @since  3.0.0
145
	 *
146
	 * @param  $field
147
	 *
148
	 * @return mixed
149
	 */
150
	private static function get_field_default_value( $field ) {
151
152
		$saved_value   = isset( $field['value'] )   ? $field['value']   : '';
153
		$default_value = isset( $field['default'] ) ? $field['default'] : '';
154
155
		return ! empty( $saved_value ) ? $saved_value : $default_value;
156
	}
157
158
}
159