-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadd-class-to-elementor-image.php
190 lines (171 loc) · 4.77 KB
/
add-class-to-elementor-image.php
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
<?php
/**
* Plugin Name: Add class to Elementor Image
* Plugin URI: https://eduardovillao.me/wordpress-plugins/
* Description: Simple plugin to add custom CSS class to Elementor image.
* Author: EduardoVillao.me
* Author URI: https://eduardovillao.me/
* Version: 1.3.2
* Requires at least: 5.5
* Requires PHP: 7.0
* Text Domain: add-class-to-elementor-image
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
/*
Add class to Elementor Image is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
any later version.
Add class to Elementor Image is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Add class to Elementor Image. If not, see http://www.gnu.org/licenses/gpl-2.0.txt.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Add class to Elementor image
*
* @since 1.0
*/
final class ACEI_Init {
/**
* Instance
*
* @since 1.0
*
* @access private
* @static
*
* @var ACEI_Init The single instance of the class.
*/
private static $_instance = null;
/**
* Instance
*
* Ensures only one instance of the class is loaded or can be loaded.
*
* @since 1.0
*
* @access public
* @static
*
* @return ACEI_Init An instance of the class.
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Disable class cloning and throw an error on object clone.
*
* The whole idea of the singleton design pattern is that there is a single
* object. Therefore, we don't want the object to be cloned.
*
* @access public
* @since 1.6
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'add-class-to-elementor-image' ), '1.3.2' );
}
/**
* Disable unserializing of the class.
*
* @access public
* @since 1.6
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'add-class-to-elementor-image' ), '1.3.2' );
}
/**
* Constructor
*
* Private method for prevent instance outsite the class.
*
* @since 1.0
*
* @access private
*/
private function __construct() {
add_action( 'plugins_loaded', [ $this, 'add_elementor_mod' ] );
}
/**
* Init Elemento mods
*
* Add Elementor mods if plugins exist and is loaded.
*
* @since 1.0
*
* @access public
*/
public function add_elementor_mod() {
if ( ! did_action( 'elementor/loaded' ) ) {
return;
}
add_action( 'elementor/element/image/section_image/before_section_end', [ $this, 'add_class_control' ], 10, 2 );
add_filter( 'elementor/image_size/get_attachment_image_html', [ $this, 'add_custom_class' ], 10, 4 );
add_action( 'elementor/element/image-box/section_image/before_section_end', [ $this, 'add_class_control' ], 10, 2 );
add_action( 'wp_loaded', [ $this, 'add_elementor_pro_mod' ] );
}
/**
* Init Elementor PRO mods
*
* Add Elementor PRO mods if the plugin exist and is loaded.
*
* @since 1.0
*
* @access public
*/
public function add_elementor_pro_mod() {
if ( ! did_action( 'elementor_pro/init' ) ) {
return;
}
add_action( 'elementor/element/theme-post-featured-image/section_image/before_section_end', [ $this, 'add_class_control' ], 10, 2 );
}
/**
* Add class control
*
* Add custom control to image widget.
*
* @since 1.0
*
* @access public
*/
public function add_class_control( $image, $args) {
$image->add_control(
'cei_image_custom_class',
[
'label' => __( 'Custom Class', 'add-class-to-elementor-image' ),
'type' => \Elementor\Controls_Manager::TEXT,
'placeholder' => __( 'your-custom-class-here', 'add-class-to-elementor-image' ),
]
);
}
/**
* Add custom class
*
* Add custom class to HTML img.
*
* @since 1.0
*
* @access public
*/
public function add_custom_class( $html, $settings, $image_size_key, $image_key ) {
if( ! isset( $settings['cei_image_custom_class'] ) && empty( $settings['cei_image_custom_class'] ) ) {
return $html;
}
$attrClass = strpos( $html, "class=" );
if ( $attrClass ) {
return preg_replace( '/class="(.*)"/', 'class="' . $settings['cei_image_custom_class'] . ' \1"', $html );
} else {
return preg_replace( '/src="(.*)"/', 'class="' . $settings['cei_image_custom_class'] . '" src="\1"', $html );
}
}
}
ACEI_Init::instance();