/var/www/vhosts/ihelp.ro/ajutam.ro/wp-content/plugins/essential-grid/includes
Edit: /var/www/vhosts/ihelp.ro/ajutam.ro/wp-content/plugins/essential-grid/includes/watermarks.class.php (22193B)
* @link http://www.themepunch.com/essential/
* @copyright 2021 ThemePunch
*/
if( !defined( 'ABSPATH') ) exit();
class Essential_Grid_Watermarks {
/**
* watermarks option name
*/
const ESG_WATERMARKS_OPTION = 'tp_eg_watermarks';
/**
* @var string
*/
private $upload_path = '/essential-grid/watermarks/';
/**
* @var array default option values
*/
private $_defaults = array(
'watermarks-enabled' => 'false',
'watermarks-library' => 'GD',
'watermarks-quality' => '92',
'watermarks-type' => 'image',
'watermarks-image' => '',
'watermarks-text' => 'Watermarked',
'watermarks-text-repeat' => 'true',
'watermarks-font' => 'Arial.ttf',
'watermarks-color' => '#FFFFFF',
'watermarks-size' => '24',
'watermarks-angle' => '30',
'watermarks-opacity' => '50',
'watermarks-position-top' => 5,
'watermarks-position-left' => 5,
'watermarks-position-width' => 35,
'watermarks-position-height' => 35,
);
/**
* @var array fonts list
*/
private $_fonts = array(
'Arial.ttf' => 'Arial',
'Arial_Black.ttf' => 'Arial Black',
'Comic_Sans_MS.ttf' => 'Comic Sans MS',
'Courier_New.ttf' => 'Courier New',
'Georgia.ttf' => 'Georgia',
'Impact.ttf' => 'Impact',
'Tahoma.ttf' => 'Tahoma',
'Times_New_Roman.ttf' => 'Times New Roman',
'Trebuchet_MS.ttf' => 'Trebuchet MS',
'Verdana.ttf' => 'Verdana',
);
protected static $instance = null;
/**
* @return Essential_Grid_Watermarks
*/
public static function get_instance()
{
if ( is_null( static::$instance ) ) {
static::$instance = new Essential_Grid_Watermarks();
}
return static::$instance;
}
protected function __construct()
{
require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php';
require_once ESG_PLUGIN_PATH . '/includes/watermarks.gd.class.php';
require_once ESG_PLUGIN_PATH . '/includes/watermarks.imagick.class.php';
$this->addActions();
$this->addFilters();
}
/**
* add actions
*/
protected function addActions()
{
//js
add_action('essgrid_enqueue_admin_scripts', array($this, 'enqueueAdminScripts'));
}
/**
* add filters
*/
protected function addFilters()
{
//global settings
add_filter('essgrid_global_settings_menu', array($this, 'addGlobalSettingsMenu'), 10, 1);
add_filter('essgrid_global_settings_content', array($this, 'addGlobalSettingsContent'), 10, 1);
//grid edit page
add_filter('essgrid_grid_create_menu', array($this, 'addGridCreateMenu'), 10, 1);
add_filter('essgrid_grid_create_settings', array($this, 'addGridCreateSettings'), 10, 2);
//set item skin media source before render
//check the sources and apply watermark if needed
add_filter('essgrid_item_skin_set_media_sources', array($this, 'applyWatermark'), 10, 3);
}
public function enqueueAdminScripts()
{
global $esg_dev_mode;
if ($esg_dev_mode) {
// DEV VERSION
wp_enqueue_script('esg-watermarks-script', plugins_url('assets/js/modules/dev/watermarks.js', ESG_PLUGIN_ADMIN_PATH . '/index.php'), array('jquery'), Essential_Grid::VERSION);
}
}
/**
* get watermarks options defaults
* @return array
*/
public function getDefaults()
{
return $this->_defaults;
}
/**
* get watermarks options
* @return array
*/
public function getOptions()
{
$options = get_option(self::ESG_WATERMARKS_OPTION, array());
if (!is_array($options)) return $this->_defaults;
return array_merge($this->_defaults, $options);
}
/**
* save options
* @param array $options
* @return bool
*/
public function saveOptions($options)
{
return update_option(self::ESG_WATERMARKS_OPTION, $options);
}
/**
* check requirements
* @return array
*/
public function checkRequirements()
{
$path = $this->getUploadPath();
return array(
'upload' => $path && wp_mkdir_p($path),
'GD' => Essential_Grid_Watermarks_GD::test(),
'GD_freetype' => Essential_Grid_Watermarks_GD::isFreetype(),
'Imagick' => Essential_Grid_Watermarks_Imagick::test(),
);
}
/**
* check if text watermark is available
* @return array
*/
public function isTextAvailable()
{
$options = $this->getOptions();
if ('GD' !== $options['watermarks-library']) return true;
$requirements = $this->checkRequirements();
return $requirements['GD'] && $requirements['GD_freetype'];
}
/**
* get upload path
* @return bool|string
*/
public function getUploadPath()
{
$upload_dir = wp_upload_dir();
if ($upload_dir['error'] != false) return false;
return $upload_dir['basedir'] . $this->upload_path;
}
/**
* get upload url
* @return bool|string
*/
public function getUploadUrl()
{
$upload_dir = wp_upload_dir();
if ($upload_dir['error'] != false) return false;
return $upload_dir['baseurl'] . $this->upload_path;
}
/**
* get fonts
* @return array
*/
public function getFonts()
{
return $this->_fonts;
}
/**
* get fonts path
* @return string
*/
public function getFontsPath()
{
return ESG_PLUGIN_ADMIN_PATH . '/assets/font';
}
/**
* get image editor object
* @param string $file
* @return Essential_Grid_Watermarks_GD | Essential_Grid_Watermarks_Imagick | bool
*/
public function getImageEditor($file)
{
$options = $this->getOptions();
$imageEditorClass = 'Essential_Grid_Watermarks_' . $options['watermarks-library'];
if (class_exists($imageEditorClass) && $imageEditorClass::test())
return new $imageEditorClass($file);
return false;
}
/**
* @param mixed $value
* @return string
*/
protected function esgRequirementsCheckmark($value)
{
if ($value) {
return '
check_circle';
} else {
return '
highlight_off';
}
}
/**
* is watermarks enabled
* check enabled option and selected image library
* @return bool
*/
public function isEnabled()
{
$options = $this->getOptions();
if ($options['watermarks-enabled'] === 'false') return false;
$requirements = $this->checkRequirements();
if (!$requirements['upload'] || !$requirements[$options['watermarks-library']]) return false;
return true;
}
/**
* add grid create menu item
* @param $str
* @return string
*/
public function addGridCreateMenu($str)
{
if (!$this->isEnabled()) return $str;
return $str
. '
'
. '' . esc_html('Watermarks', ESG_TEXTDOMAIN) . '
'
. '';
}
/**
* add grid create settings section
* @param string $str
* @param mixed $grid
* @return string
*/
public function addGridCreateSettings($str, $grid)
{
if (!$this->isEnabled()) return $str;
//get global options and turn off watermarks by default
$options = $this->getOptions();
$options['watermarks-enabled'] = 'false';
$options['watermarks-override-defaults'] = 'false';
//load options from grid
if (isset($grid['params']['watermarks-enabled'])) $options = array_merge($options, $grid['params']);
$content =
'
'
. $this->getStatusSectionHtml($options['watermarks-enabled'])
. '
'
. '
' . esc_html('Use Defaults', ESG_TEXTDOMAIN) .'
'
. '
'
. '
'
. $this->getWatermarkSectionHtml($options, 'Watermark Settings')
. '
';
return $str . $content;
}
/**
* add global settings menu item
* @param $str
* @return string
*/
public function addGlobalSettingsMenu($str)
{
return $str
. '
'
. 'water_drop' . esc_html('Watermarks', ESG_TEXTDOMAIN) . '
'
. '';
}
/**
* add global settings content section
* @param $str
* @return string
*/
public function addGlobalSettingsContent($str)
{
$options = $this->getOptions();
$requirements = $this->checkRequirements();
$options_library = '';
if ($requirements['Imagick']) {
$options_library .= '
';
}
if ($requirements['GD']) {
$options_library .= '
';
}
$content =
'
'
. '
'
. '
' . esc_html('Requirements', ESG_TEXTDOMAIN) . '
'
. '
'
. '
'
. '
'
. '- ' . $this->esgRequirementsCheckmark($requirements['upload']) . ' Upload dir writable
'
. '
'
. '
'
. '
'
. '
'
. '- ' . $this->esgRequirementsCheckmark($requirements['GD']) . ' GD
'
. '- ' . $this->esgRequirementsCheckmark($requirements['GD_freetype']) . ' GD FreeType ( Required for text watermarks )
'
. '- ' . $this->esgRequirementsCheckmark($requirements['Imagick']) . ' ImageMagick
'
. '
'
. '
'
. '
'
. $this->getStatusSectionHtml($options['watermarks-enabled'])
. '
'
. '
' . esc_html('Settings', ESG_TEXTDOMAIN) . '
'
. '
'
. '
'
. '
'
. '
' . esc_html('Select PHP library to process images', ESG_TEXTDOMAIN) . ''
. '
'
. '
'
. '
'
. '
' . esc_html('Set JPEG quality. 1 = worst quality / smaller size, 100 = best quality / bigger size', ESG_TEXTDOMAIN) . ''
. '
'
. '
'
. $this->getWatermarkSectionHtml($options)
. '
';
return $str . $content;
}
/**
* get status section html
* @param string $enabled
* @return string
*/
protected function getStatusSectionHtml($enabled)
{
return
'
'
. '
' . esc_html('Status', ESG_TEXTDOMAIN) .'
'
. '
'
. '
';
}
/**
* get watermark settings section html
* @param array $options
* @param string $section_title
* @return string
*/
protected function getWatermarkSectionHtml($options, $section_title = 'Default Watermark')
{
$globalOptions = $this->getOptions();
//disable text option
if (!$this->isTextAvailable()) {
$options['watermarks-type'] = 'image';
}
$fonts = $this->getFonts();
$options_fonts = '';
foreach ($fonts as $key => $val) {
$options_fonts .= '
';
}
$handle_style = 'width:' . ($options['watermarks-position-width'] * 2) . 'px;'
. 'height:' . ($options['watermarks-position-height'] * 2) . 'px;'
. 'top:' . ($options['watermarks-position-top'] * 2) . 'px;'
. 'left:' . ($options['watermarks-position-left'] * 2) . 'px;';
$image_src = wp_get_attachment_image_src($options['watermarks-image'], 'large');
$image_src = !empty($image_src) ? $image_src[0] : '';
return
'
'
. '
' . esc_html($section_title, ESG_TEXTDOMAIN) . '
'
. '
'
. '
' . esc_html('Image', ESG_TEXTDOMAIN) . '' . esc_html('Text', ESG_TEXTDOMAIN) . ''
. '
'
. '
'
. '
'
. '
'
. '
'
. '
' . esc_html('Set watermark image', ESG_TEXTDOMAIN) . ''
. '
'
. '

'
. '
'
. '
'
. '
%'
. '
'
. '
' . esc_html('Opacity is not applied to png images.', ESG_TEXTDOMAIN) . ''
. '
'
. '
'
. '
'
. '
'
. '
'
. 'Width:
' . esc_html($options['watermarks-position-width']) . '%'
. '
'
. 'Height:
' . esc_html($options['watermarks-position-height']) . '%'
. '
'
. 'Top:
' . esc_html($options['watermarks-position-top']) . '%'
. '
'
. 'Left:
' . esc_html($options['watermarks-position-left']) . '%'
. '
'
. '
'
. '
'
. '
'
. '
'
. '
'
. '
'
. '
'
. '
'
. '
'
. '
'
. '
' . esc_html('On', ESG_TEXTDOMAIN) . '' . esc_html('Off', ESG_TEXTDOMAIN) . ''
. '
'
. '
'
. '
'
. '
'
. '
'
. '
pt'
. '
'
. '
' . esc_html('Text might be not rendered with too big font size. Try to lower the size in this case.', ESG_TEXTDOMAIN) . ''
. '
'
. '
∠'
. '
'
. '
'
. '
'
. '
';
}
/**
* apply watermark to media sources if needed
* @param array $sources
* @param mixed $grid_id
* @param array $grid_params
* @return mixed
*/
public function applyWatermark($sources, $grid_id, $grid_params)
{
//no grid id
if (empty($grid_id)) return $sources;
//cant be converted to string
if (!is_scalar($grid_id)) return $sources;
$is_enabled = Essential_Grid_Base::getVar($grid_params, 'watermarks-enabled', 'false');
if ($is_enabled !== 'true') return $sources;
foreach ($sources as $key => $val) {
if (empty($val) || !is_string($val)) continue;
$editor = $this->getImageEditor($val);
$loaded = $editor->load();
if (is_wp_error($loaded)) continue;
$watermarked = $editor->apply_watermark($grid_id, $grid_params);
//process error?
if (empty($watermarked) || is_wp_error($watermarked)) continue;
$sources[$key] = $watermarked;
$sources[$key . '-original'] = $val;
}
return $sources;
}
/**
* @param mixed $grid_id
* @param array $params
* @param string $file
* @return bool|array
*/
public function getFileData($grid_id, $params, $file)
{
$file_new = $grid_id . '/' . md5(serialize($params)) . '/' . basename($file);
return [
'path' => $this->getUploadPath() . $file_new,
'url' => $this->getUploadUrl() . $file_new,
];
}
}
Essential_Grid_Watermarks::get_instance();