@@ -16,9 +16,12 @@ class EnableMediaReplacePlugin
1616 private $ user_cap = false ;
1717 private $ general_cap = false ;
1818
19+ private $ features = array ();
20+
1921 public function __construct ()
2022 {
21- add_action ('plugins_loaded ' , array ($ this , 'runtime ' ));
23+ add_action ('plugins_loaded ' , array ($ this , 'runtime ' )); //lowInit, before theme setup!
24+ add_action ('admin_init ' , array ($ this , 'adminInit ' )); // adminInit, after functions.php
2225 }
2326
2427 public function runtime ()
@@ -43,9 +46,17 @@ public function runtime()
4346 return ;
4447 }
4548
49+
50+
4651 $ this ->plugin_actions (); // init
4752 }
4853
54+ public function adminInit ()
55+ {
56+ $ this ->features ['replace ' ] = true ; // does nothing just for completeness
57+ $ this ->features ['background ' ] = apply_filters ('emr/feature/background ' , true );
58+ }
59+
4960 public function filesystem ()
5061 {
5162 return new FileSystem ();
@@ -56,6 +67,20 @@ public function uiHelper()
5667 return Uihelper::getInstance ();
5768 }
5869
70+ public function useFeature ($ name )
71+ {
72+ switch ($ name )
73+ {
74+ case 'background ' :
75+ $ bool = $ this ->features ['background ' ];
76+ break ;
77+ default :
78+ $ bool = false ;
79+ break ;
80+ }
81+ return $ bool ;
82+ }
83+
5984 public static function get ()
6085 {
6186 if (is_null (self ::$ instance )) {
@@ -106,7 +131,7 @@ public function plugin_actions()
106131 add_action ('wp_ajax_emr_dismiss_notices ' , array ($ this ,'dismiss_notices ' ));
107132
108133 // editors
109- add_action ('add_meta_boxes ' , array ($ this , 'add_meta_boxes ' ), 10 , 2 );
134+ add_action ('add_meta_boxes_attachment ' , array ($ this , 'add_meta_boxes ' ), 10 , 2 );
110135 add_filter ('attachment_fields_to_edit ' , array ($ this , 'attachment_editor ' ), 10 , 2 );
111136
112137 /** Just after an image is replaced, try to browser decache the images */
@@ -131,7 +156,6 @@ public function menu()
131156 $ title = esc_html__ ("Replace media " , "enable-media-replace " );
132157 $ title = (isset ($ _REQUEST ['action ' ]) && ($ _REQUEST ['action ' ] === 'emr_prepare_remove ' )) ? esc_html__ ("Remove background " , "enable-media-replace " ) : $ title ;
133158 add_submenu_page ('upload.php ' ,$ title , $ title , 'upload_files ' , 'enable-media-replace/enable-media-replace.php ' , array ($ this , 'route ' ));
134- /* add_submenu_page(null, esc_html__("Remove background", "enable-media-replace"), esc_html__("Remove the media Background", "enable-media-replace"), 'upload_files', 'emr-remove-background', array($this, 'route')); */
135159
136160
137161 }
@@ -195,9 +219,6 @@ public function route()
195219
196220 $ this ->uiHelper ()->featureNotice ();
197221
198-
199-
200-
201222 if (! check_admin_referer ($ action , '_wpnonce ' )) {
202223 die ('Invalid Nonce ' );
203224 }
@@ -212,7 +233,7 @@ public function route()
212233 elseif ($ action == 'media_replace_upload ' ) {
213234 require_once ($ this ->plugin_path . 'views/upload.php ' );
214235 }
215- elseif ('emr_prepare_remove ' === $ action ) {
236+ elseif ('emr_prepare_remove ' === $ action && $ this -> useFeature ( ' background ' ) ) {
216237 $ attachment_id = intval ($ _GET ['attachment_id ' ]);
217238 $ attachment = get_post ($ attachment_id );
218239 //We're adding a timestamp to the image URL for cache busting
@@ -224,7 +245,10 @@ public function route()
224245 wp_enqueue_script ('emr_upsell ' );
225246 require_once ($ this ->plugin_path . "views/prepare-remove-background.php " );
226247
227- } elseif ('do_background_replace ' === $ action && check_admin_referer ($ action , '_wpnonce ' )) {
248+ } elseif ('do_background_replace ' === $ action &&
249+ check_admin_referer ($ action , '_wpnonce ' ) &&
250+ $ this ->useFeature ('background ' )
251+ ) {
228252 require_once ($ this ->plugin_path . 'views/do-replace-background.php ' );
229253 }
230254 else {
@@ -311,8 +335,24 @@ public function convertdate($sFormat)
311335 }
312336 }
313337
314- public function checkImagePermission ($ author_id , $ post_id )
338+ public function checkImagePermission ($ post )
315339 {
340+ if (! is_object ($ post ))
341+ {
342+ return false ;
343+ }
344+ $ post_id = $ post ->ID ;
345+ $ post_type = $ post ->post_type ;
346+ $ author_id = $ post ->post_author ;
347+
348+ if ($ post_type !== 'attachment ' )
349+ return false ;
350+
351+ if (is_null ($ post_id ) || intval ($ post_id ) >! 0 )
352+ {
353+ return false ;
354+ }
355+
316356 if ($ this ->general_cap === false && $ this ->user_cap === false ) {
317357 if (current_user_can ('edit_post ' , $ post_id ) === true ) {
318358 return true ;
@@ -354,14 +394,14 @@ protected function getRemoveBgURL($attach_id)
354394 return $ url ;
355395 }
356396
357- public function add_meta_boxes ($ post_type , $ post )
397+ public function add_meta_boxes ($ post )
358398 {
359399 // Because some plugins don't like to play by the rules.
360- if (is_null ($ post_type ) || is_null ($ post )) {
400+ if (is_null ($ post ) || ! is_object ($ post ) ) {
361401 return false ;
362402 }
363403
364- if (! $ this ->checkImagePermission ($ post-> post_author , $ post -> ID )) {
404+ if (! $ this ->checkImagePermission ($ post )) {
365405 return ;
366406 }
367407
@@ -400,7 +440,7 @@ public function replace_meta_box($post)
400440
401441 public function show_thumbs_box ($ post )
402442 {
403- if (! $ this ->checkImagePermission ($ post-> post_author , $ post -> ID )) {
443+ if (! $ this ->checkImagePermission ($ post )) {
404444 return ;
405445 }
406446
@@ -435,7 +475,7 @@ public function attachment_editor($form_fields, $post)
435475 {
436476 $ screen = null ;
437477
438- if (! $ this ->checkImagePermission ($ post-> post_author , $ post -> ID )) {
478+ if (! $ this ->checkImagePermission ($ post )) {
439479 return $ form_fields ;
440480 }
441481
@@ -489,7 +529,7 @@ public function add_mime_types($mime_types)
489529 public function add_media_action ($ actions , $ post )
490530 {
491531
492- if (! $ this ->checkImagePermission ($ post-> post_author , $ post -> ID )) {
532+ if (! $ this ->checkImagePermission ($ post )) {
493533 return $ actions ;
494534 }
495535
0 commit comments