PluginProbe ʕ •ᴥ•ʔ
Starter Sites & Templates by Neve / 1.4.1
Starter Sites & Templates by Neve v1.4.1
1.4.1 1.4.0 1.3.0 1.2.29 1.2.28 1.2.6 1.2.7 1.2.8 1.2.9 trunk 1.0.10 1.0.11 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.27 1.1.28 1.1.29 1.1.3 1.1.30 1.1.31 1.1.32 1.1.33 1.1.34 1.1.35 1.1.36 1.1.37 1.1.38 1.1.39 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.3 1.2.4 1.2.5
templates-patterns-collection / includes / Importers / Cleanup / Active_State.php
templates-patterns-collection / includes / Importers / Cleanup Last commit date
Active_State.php 1 year ago Manager.php 3 months ago
Active_State.php
163 lines
1 <?php
2 /**
3 * The plugin active state for cleanup.
4 *
5 * Used to keep track of the state of the import for cleanup.
6 *
7 * @package templates-patterns-collection
8 */
9 namespace TIOB\Importers\Cleanup;
10
11 /**
12 * Class Active_State
13 * @package TIOB\Importers\Cleanup
14 */
15 class Active_State {
16
17 const STATE_NAME = 'neve_last_imports';
18 const HOUR_IN_SECONDS = 3600;
19 const PLUGINS_NSP = 'plugins';
20 const CATEGORY_NSP = 'category';
21 const TAGS_NSP = 'tags';
22 const TERMS_NSP = 'terms';
23 const POSTS_NSP = 'posts';
24 const COMMENTS_NSP = 'comments';
25 const ATTACHMENT_NSP = 'attachment';
26 const FRONT_PAGE_NSP = 'front_page_options';
27 const SHOP_PAGE_NSP = 'shop_page_options';
28 const PAYMENT_FORM_NSP = 'payment_form_options';
29 const THEME_MODS_NSP = 'theme_mods';
30 const MENUS_NSP = 'menus';
31 const WIDGETS_NSP = 'widgets';
32 /**
33 * @var array $state
34 */
35 private $state;
36
37 /**
38 * Active_State constructor.
39 */
40 final public function __construct() {
41 $this->fresh_state();
42 }
43
44 /**
45 * Init the active state called from Main.
46 */
47 final public function init() {
48 add_action( 'themeisle_cl_add_property_state', array( $this, 'add_property_state' ), 10, 2 );
49 add_action( 'themeisle_cl_add_item_to_property_state', array( $this, 'add_item_to_property_state' ), 10, 2 );
50 }
51
52 private function is_allowed_property( $property_key ) {
53 return in_array(
54 $property_key,
55 array(
56 self::PLUGINS_NSP,
57 self::CATEGORY_NSP,
58 self::TAGS_NSP,
59 self::TERMS_NSP,
60 self::POSTS_NSP,
61 self::COMMENTS_NSP,
62 self::ATTACHMENT_NSP,
63 self::THEME_MODS_NSP,
64 self::MENUS_NSP,
65 self::WIDGETS_NSP,
66 self::FRONT_PAGE_NSP,
67 self::SHOP_PAGE_NSP,
68 self::PAYMENT_FORM_NSP,
69 ),
70 true
71 );
72 }
73
74 final public function add_property_state( $property_key, $data ) {
75 if ( $this->is_allowed_property( $property_key ) ) {
76 $this->add( $property_key, $data );
77 }
78 }
79
80 final public function add_item_to_property_state( $property_key, $item ) {
81 if ( $this->is_allowed_property( $property_key ) ) {
82 $property = $this->get_by_key( $property_key );
83 if ( empty( $property ) ) {
84 $property = array();
85 }
86 $property[] = $item;
87 $this->add( $property_key, $property );
88 }
89 }
90
91 /**
92 * Make sure state is fresh.
93 *
94 * @return void
95 */
96 private function fresh_state() {
97 $state = get_transient( self::STATE_NAME );
98 if ( empty( $state ) ) {
99 $this->state = array();
100 }
101 $this->state = $state;
102 }
103
104 /**
105 * Get current active state.
106 *
107 * @return array|null
108 */
109 final public function get() {
110 $this->fresh_state();
111 return $this->state;
112 }
113
114 /**
115 * Add to state method.
116 *
117 * @param string $key State key.
118 * @param mixed $data Data for the state key.
119 * @return void
120 */
121 final public function add( $key, $data ) {
122 if ( empty( $this->state ) ) {
123 $this->state = array();
124 }
125 $this->state[ $key ] = $data;
126 set_transient( self::STATE_NAME, $this->state, 24 * self::HOUR_IN_SECONDS );
127 }
128
129 /**
130 * Remove from state method.
131 *
132 * @param string $key
133 * @return void
134 */
135 final public function remove( $key ) {
136 unset( $this->state[ $key ] );
137 set_transient( self::STATE_NAME, $this->state, 24 * self::HOUR_IN_SECONDS );
138 }
139
140 /**
141 * Return specific key from state.
142 *
143 * @param string $key State key.
144 * @return mixed|null
145 */
146 final public function get_by_key( $key ) {
147 $this->fresh_state();
148 if ( empty( $this->state[ $key ] ) ) {
149 return null;
150 }
151 return $this->state[ $key ];
152 }
153
154 /**
155 * Method to get set keys.
156 * @return string[]
157 */
158 final public function get_set_keys() {
159 $this->fresh_state();
160 return array_keys( $this->state );
161 }
162 }
163