Skip to content

Commit d7a144f

Browse files
authored
Release version 4.0.1 (#87)
Merge pull request #87 from short-pixel-optimizer/updates
2 parents fdf3ff5 + 804151e commit d7a144f

File tree

16 files changed

+197
-50
lines changed

16 files changed

+197
-50
lines changed

build/shortpixel/filesystem/src/Controller/FileSystemController.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ public function getWPAbsPath()
7575
return $this->getDirectory($abspath);
7676
}
7777

78+
public function getFullPathForWP(FileModel $file)
79+
{
80+
$fullpath = $file->getFullPath();
81+
$abspath = $this->getWPAbsPath();
82+
83+
if (! strpos($abspath, $fullpath))
84+
{
85+
86+
}
87+
88+
}
89+
7890

7991
/** Utility function that tries to convert a file-path to a webURL.
8092
*
@@ -85,8 +97,11 @@ public function pathToUrl(FileModel $file)
8597
$filepath = $file->getFullPath();
8698
$directory = $file->getFileDir();
8799

88-
$is_multi_site = $this->env->is_multisite;
89-
$is_main_site = $this->env->is_mainsite;
100+
$is_multi_site = (function_exists("is_multisite") && is_multisite()) ? true : false;
101+
$is_main_site = is_main_site();
102+
103+
//$is_multi_site = $this->env->is_multisite;
104+
//$is_main_site = $this->env->is_mainsite;
90105

91106
// stolen from wp_get_attachment_url
92107
if ( ( $uploads = wp_get_upload_dir() ) && (false === $uploads['error'] || strlen(trim($uploads['error'])) == 0 ) ) {
@@ -113,6 +128,9 @@ public function pathToUrl(FileModel $file)
113128

114129
} elseif ( false !== strpos( $filepath, 'wp-content/uploads' ) ) {
115130
// Get the directory name relative to the basedir (back compat for pre-2.7 uploads)
131+
//$relativePath = $this->getFile(_wp_get_attachment_relative_path( $filepath ) );
132+
//$basename = wp_basename($relativePath->getFullPath());
133+
116134
$url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $filepath ) ) . wp_basename( $filepath );
117135
} else {
118136
// It's a newly-uploaded file, therefore $file is relative to the basedir.
@@ -130,7 +148,7 @@ public function pathToUrl(FileModel $file)
130148
// (2) ** Also a real life fix when a path is /wwwroot/assets/sites/2/ etc, in get site url, the home URL is the site URL, without appending the sites stuff. Fails on original image.
131149
if ($is_multi_site && ! $is_main_site)
132150
{
133-
$wp_home_path = wp_normalize_path(trailingslashit($uploads['basedir']));
151+
$wp_home_path = trailingslashit($uploads['basedir']);
134152
$home_url = trailingslashit($uploads['baseurl']);
135153
}
136154
else

build/shortpixel/filesystem/src/Model/File/DirectoryModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class DirectoryModel
3232
*/
3333
public function __construct($path)
3434
{
35-
$path = wp_normalize_path($path);
35+
// $path = wp_normalize_path($path);
3636
$fs = $this->getFS();
3737

3838
if ($fs->pathIsUrl($path))

build/shortpixel/filesystem/src/Model/File/FileModel.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class FileModel
1717

1818
// File info
1919
protected $fullpath = null;
20+
protected $rawfullpath = null;
2021
protected $filename = null; // filename + extension
2122
protected $filebase = null; // filename without extension
2223
protected $directory = null;
@@ -42,7 +43,9 @@ class FileModel
4243
/** Creates a file model object. FileModel files don't need to exist on FileSystem */
4344
public function __construct($path)
4445
{
46+
4547
$this->fullpath = trim($path);
48+
$this->rawfullpath = $this->fullpath; // path without any doing.
4649
$fs = $this->getFS();
4750
if ($fs->pathIsUrl($path)) // Asap check for URL's to prevent remote wrappers from running.
4851
{
@@ -377,7 +380,7 @@ public function getFullPath()
377380
// So far, testing use for file Filter */
378381
public function getRawFullPath()
379382
{
380-
return $this->fullpath;
383+
return $this->rawfullpath;
381384
}
382385

383386
public function getFileName()
@@ -440,7 +443,7 @@ protected function processPath($path)
440443
if ($path === false) // don't process further
441444
return false;
442445

443-
$path = wp_normalize_path($path);
446+
//$path = wp_normalize_path($path);
444447
$abspath = $fs->getWPAbsPath();
445448

446449
if ( is_file($path) && ! is_dir($path) ) // if path and file exist, all should be okish.

build/shortpixel/log/src/DebugItem.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,25 @@ public function __construct($message, $args)
4141
{
4242
$this->data[] = print_r($data, true);
4343
}
44-
if ($dataType == 2) //array
44+
if ($dataType == 2) //array or object.
4545
{
46+
$count = false;
47+
if (gettype($data) == 'array')
48+
$count = count($data);
49+
elseif(gettype($data) == 'object')
50+
$count = count(get_object_vars($data));
51+
52+
$firstLine = ucfirst(gettype($data)) . ':';
53+
if ($count !== false)
54+
$firstLine .= ' (' . $count . ')';
55+
56+
$this->data[] = $firstLine;
57+
4658
foreach($data as $index => $item)
4759
{
4860
if (is_object($item) || is_array($item))
4961
{
50-
$this->data[] = print_r($item, true);
62+
$this->data[] = print_r($index, true) . ' ( ' . ucfirst(gettype($item)) . ') => ' . print_r($item, true);
5163
}
5264
}
5365
}

build/shortpixel/notices/src/NoticeModel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ public function getForDisplay()
234234
document.getElementById('button-$id').onclick = function()
235235
{
236236
var el = document.getElementById('$id');
237-
$(el).fadeTo(100,0,function() {
238-
$(el).slideUp(100, 0, function () {
239-
$(el).remove();
237+
jQuery(el).fadeTo(100,0,function() {
238+
jQuery(el).slideUp(100, 0, function () {
239+
jQuery(el).remove();
240240
})
241241
});
242242
} </script>";

classes/emr-plugin.php

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

classes/external/siteorigin.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace EnableMediaReplace\Externals;
3+
4+
5+
class SiteOrigin
6+
{
7+
protected static $instance;
8+
9+
public function __construct()
10+
{
11+
if (defined('SITEORIGIN_PANELS_VERSION'))
12+
{
13+
add_filter('emr/replacer/option_fields', array($this, 'addOption'));
14+
}
15+
}
16+
17+
public static function getInstance()
18+
{
19+
if (is_null(self::$instance))
20+
{
21+
self::$instance = new SiteOrigin();
22+
}
23+
24+
return self::$instance;
25+
}
26+
27+
public function addOption($options)
28+
{
29+
$options[] = 'widget_siteorigin-panels-builder';
30+
return $options;
31+
}
32+
} // class

classes/externals.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
use EnableMediaReplace\Externals\Elementor as Elementor;
77
use EnableMediaReplace\Externals\WpBakery as WpBakery;
8+
use EnableMediaReplace\Externals\SiteOrigin as SiteOrigin;
9+
810

911
class Externals
1012
{
@@ -25,6 +27,7 @@ public function __construct()
2527
// integrations
2628
$this->loadElementor();
2729
$this->loadBakery(); // in case of urlencoded issues, this class should be used probably.
30+
$this->loadSiteOrigins();
2831
}
2932

3033
protected function check() // check if any of the options should be disabled due to conflicts
@@ -70,4 +73,9 @@ public function loadBakery()
7073
WpBakery::getInstance();
7174
}
7275

76+
public function loadSiteOrigins()
77+
{
78+
SiteOrigin::getInstance();
79+
}
80+
7381
} // class

0 commit comments

Comments
 (0)