1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
<?php namespace WpAssetCleanUp;
/** * Class MetaBoxes * @package WpAssetCleanUp */ class MetaBoxes { /** * @var array */ public static $noMetaBoxesForPostTypes = array( // Oxygen Page Builder 'ct_template', 'oxy_user_library',
// Themify Page Builder (Layout & Layout Part) 'tbuilder_layout', 'tbuilder_layout_part',
// "Popup Maker" plugin 'popup', 'popup_theme',
// "Popup Builder" plugin 'popupbuilder',
// "Datafeedr Product Sets" plugin 'datafeedr-productset' );
/** * */ public function initMetaBox($type) { if ( ! Menu::userCanManageAssets() ) { return; }
if ( Main::instance()->settings['allow_manage_assets_to'] === 'chosen' && ! empty(Main::instance()->settings['allow_manage_assets_to_list']) ) { $wpacuCurrentUserId = get_current_user_id();
if ( ! in_array( $wpacuCurrentUserId, Main::instance()->settings['allow_manage_assets_to_list'] ) ) { return; // the current logged-in admin is not in the list of "Allow managing assets to:" } }
if ($type === 'manage_page_assets') { add_action( 'add_meta_boxes', array( $this, 'addAssetManagerMetaBox' ), 11 ); add_action( 'add_meta_boxes', array( $this, 'keepAssetManagerMetaBoxOnTheLeftSide' ), 1 ); }
if ($type === 'manage_page_options') { add_action( 'add_meta_boxes', array( $this, 'addPageOptionsMetaBox' ), 12 ); } }
/** * @param $postType */ public function addAssetManagerMetaBox($postType) { $obj = $this->showMetaBoxes($postType);
if (isset($obj->public) && $obj->public > 0) { add_meta_box( WPACU_PLUGIN_ID . '_asset_list', WPACU_PLUGIN_TITLE.': '.__('CSS & JavaScript Manager', 'wp-asset-clean-up'), array($this, 'renderAssetManagerMetaBoxContent'), $postType, apply_filters('wpacu_asset_list_meta_box_context', 'normal'), apply_filters('wpacu_asset_list_meta_box_priority', 'high') ); } }
/** * Sometimes, users are moving by mistake the meta box to the right side which is not desirable * and have difficulties moving it back, thus, this method moves it back to the left (normal) side * * @param $postType */ public function keepAssetManagerMetaBoxOnTheLeftSide($postType) { $user = wp_get_current_user();
if (isset($user->ID) && $user->ID) { $userMetaBoxOption = get_user_option('meta-box-order_'.$postType, $user->ID );
if (isset($userMetaBoxOption['side'], $userMetaBoxOption['normal']) && strpos($userMetaBoxOption['side'], WPACU_PLUGIN_ID . '_asset_list') !== false) { // Remove it from the side list if (strpos($userMetaBoxOption['side'], ',') !== false) { $allSideMetaBoxes = explode(',', $userMetaBoxOption['side']);
foreach ($allSideMetaBoxes as $sideMetaBoxIndex => $sideMetaBoxName) { if ($sideMetaBoxName === WPACU_PLUGIN_ID . '_asset_list') { unset($allSideMetaBoxes[$sideMetaBoxIndex]); } }
$userMetaBoxOption['side'] = implode(',', array_unique($allSideMetaBoxes)); } else { $userMetaBoxOption['side'] = str_replace(WPACU_PLUGIN_ID . '_asset_list', '', $userMetaBoxOption['side']); }
// Move it back to the normal one if (strpos($userMetaBoxOption['normal'], ',') !== false) { $allNormalMetaBoxes = explode( ',', $userMetaBoxOption['normal'] ); $allNormalMetaBoxes[] = WPACU_PLUGIN_ID . '_asset_list'; $userMetaBoxOption['normal'] = implode(',', array_unique($allNormalMetaBoxes)); } elseif ($userMetaBoxOption['normal'] !== '') { $userMetaBoxOption['normal'] .= ','.WPACU_PLUGIN_ID . '_asset_list'; } elseif ($userMetaBoxOption['normal'] === '') { $userMetaBoxOption['normal'] .= WPACU_PLUGIN_ID . '_asset_list'; }
update_user_option($user->ID, 'meta-box-order_'.$postType, $userMetaBoxOption, true); } } }
/** * @param $postType */ public function addPageOptionsMetaBox($postType) { if ($this->isMediaWithPermalinkDeactivated()) { return; }
$obj = $this->showMetaBoxes($postType);
if (isset($obj->public) && $obj->public > 0) { add_meta_box( WPACU_PLUGIN_ID . '_page_options', WPACU_PLUGIN_TITLE.': '.__('Options', 'wp-asset-clean-up'), array($this, 'renderPageOptionsMetaBoxContent'), $postType, apply_filters('wpacu_page_options_meta_box_context', 'side'), apply_filters('wpacu_page_options_meta_box_priority', 'high') ); } }
/** * This is triggered only in the Edit Mode Dashboard View */ public function renderAssetManagerMetaBoxContent() { global $post;
if ($post->ID === null) { return; }
$data = array('status' => 1);
$postId = (isset($post->ID) && $post->ID > 0) ? $post->ID : 0;
$isListFetchable = true;
if (! Main::instance()->settings['dashboard_show']) { $isListFetchable = false; $data['status'] = 2; // "Manage within Dashboard" is disabled in plugin's settings } elseif ($postId < 1 || ! in_array(get_post_status($postId), array('publish', 'private'))) { $data['status'] = 3; // "draft", "auto-draft" post (it has to be published) $isListFetchable = false; }
if ($this->isMediaWithPermalinkDeactivated()) { $isListFetchable = false; $data['status'] = 4; // "Redirect attachment URLs to the attachment itself?" is enabled in "Yoast SEO" -> "Media" }
if ($isListFetchable) { $data['fetch_url'] = Misc::getPageUrl($postId);
// Check if Asset CleanUp Pro is meant to be loaded in the targeted URL // The rules from "Settings" -> "Plugin Usage Preferences" -> "Do not load the plugin on certain pages" will be checked if (assetCleanUpHasNoLoadMatches($data['fetch_url'])) { $isListFetchable = false; $data['status'] = 5; // Asset CleanUp Pro is deactivated from loading any of its rules on this page } }
$data['is_list_fetchable'] = $isListFetchable; $data['fetch_assets_on_click'] = false;
if ($isListFetchable) { if (Main::instance()->settings['assets_list_show_status'] === 'fetch_on_click') { $data['fetch_assets_on_click'] = true; }
$data['dom_get_type'] = Main::instance()->settings['dom_get_type']; }
Main::instance()->parseTemplate('meta-box', $data, true); }
/** * This is triggered only in the Edit Mode Dashboard View */ public function renderPageOptionsMetaBoxContent() { $data = array('page_options' => self::getPageOptions());
Main::instance()->parseTemplate('meta-box-side-page-options', $data, true); }
/** * @param int $postId * * @return array|mixed|object */ public static function getPageOptions($postId = 0) { if ($postId < 1) { global $post; $postId = (int)$post->ID; }
if ($postId > 1) { $metaPageOptionsJson = get_post_meta($postId, '_'.WPACU_PLUGIN_ID.'_page_options', true);
return @json_decode( $metaPageOptionsJson, ARRAY_A ); }
return array(); }
/** * @return mixed|void */ public static function hideMetaBoxesForPostTypes() { $allValues = self::$noMetaBoxesForPostTypes;
$hideForChosenPostTypes = Main::instance()->settings['hide_meta_boxes_for_post_types'];
if (! empty($hideForChosenPostTypes)) { foreach ($hideForChosenPostTypes as $chosenPostType) { $allValues[] = trim($chosenPostType); } }
return $allValues; }
/** * Determine whether to show any Asset CleanUp (Pro) meta boxes, depending on the post type * * @param $postType * @return bool|object */ public function showMetaBoxes($postType) { $obj = get_post_type_object($postType);
// These are not public pages that are loading CSS/JS // e.g. URI request ending in '/ct_template/inner-content/' if (isset($obj->name) && $obj->name && in_array($obj->name, self::hideMetaBoxesForPostTypes())) { return false; }
if (isset($_GET['post'], $obj->name) && $_GET['post'] && $obj->name) { $permalinkStructure = get_option( 'permalink_structure' ); $postPermalink = get_permalink( $_GET['post'] );
if (strpos($permalinkStructure, '%postname%') !== false && strpos($postPermalink, '/?'.$obj->name.'=')) { // Doesn't have the right permalink; Showing any Asset CleanUp (Lite or Pro) options is not relevant return false; } }
return $obj; }
/** * @return bool */ public function isMediaWithPermalinkDeactivated() { global $post;
if (method_exists('WPSEO_Options', 'get') && 'attachment' === get_post_type($post->ID)) { try { if (\WPSEO_Options::get( 'disable-attachment' ) === true) { return true; } } catch (\Exception $e) {} }
return false; } }
|