PluginProbe
BetterLinks – Link Shortener, Link Cloaking, Redirects, Affiliate Link Manager & MCP / trunk
BetterLinks – Link Shortener, Link Cloaking, Redirects, Affiliate Link Manager & MCP vtrunk
3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 3.0.0 2.4.13 2.4.12 2.4.11 2.4.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 All 110 releases
betterlinks / includes / Elementor.php

Elementor.php in BetterLinks – Link Shortener, Link Cloaking, Redirects, Affiliate Link Manager & MCP trunk, at includes/Elementor.php

363 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BetterLinks;
4 if ( ! defined( 'ABSPATH' ) ) { exit; }
5
6 use Elementor\Controls_Manager;
7 use Elementor\Plugin;
8
9 /**
10 * Summery - BetterLinks widgets for Elementor Page Builder
11 */
12 class Elementor {
13 use \BetterLinks\Traits\Links;
14 use \BetterLinks\Traits\Terms;
15 use \BetterLinks\Traits\ArgumentSchema;
16
17 /**
18 * Initializeing actions
19 */
20 public function __construct() {
21 add_action( 'betterlinks/pre_before_redirect', array( $this, 'disable_elementor_preview_redirect' ) );
22
23 if ( $this->bl_get_link_options( 'is_allow_gutenberg' ) ) {
24 add_action( 'elementor/editor/after_enqueue_scripts', array( $this, 'elementor_editor_assets' ) );
25 add_action( 'elementor/documents/register_controls', array( $this, 'instant_redirect_controls' ) );
26 add_action( 'elementor/editor/after_save', array( $this, 'handle_instant_redirect_data' ), 10 );
27 add_filter( 'elementor/document/save/data', array( $this, 'handle_page_data' ) );
28 }
29 }
30
31 /**
32 * Enqueue Elementor Editor Page's Assets
33 *
34 * @return void
35 */
36 public function elementor_editor_assets() {
37 wp_enqueue_style( 'bl-el-editor', BETTERLINKS_ASSETS_URI . 'css/elementor.css', array(), BETTERLINKS_VERSION );
38 }
39
40 /**
41 * Get Link Options
42 *
43 * @param string $option_name - Option name.
44 * @return array
45 */
46 public function bl_get_link_options( $option_name = null ) {
47 $links_option = json_decode( get_option( BETTERLINKS_LINKS_OPTION_NAME ), true );
48
49 if ( $option_name ) {
50 if ( isset( $links_option[ $option_name ] ) ) {
51 return $links_option[ $option_name ];
52 }
53
54 return '';
55 }
56
57 return $links_option;
58 }
59
60 /**
61 * Returns Category Options
62 *
63 * @param boolean $first_index - First Index.
64 * @return array - Options
65 */
66 public function bl_get_category_options( $first_index = false ) {
67 $terms = $this->get_all_terms_data( null );
68 $options = array();
69 $index = 0;
70 foreach ( (array) $terms as $term ) {
71 if ( 'tags' === $term['term_type'] ) {
72 continue;
73 }
74 if ( $first_index && 0 === $index ) {
75 ++$index;
76 return $term['ID'];
77 }
78 $options[ $term['ID'] ] = $term['term_name'];
79 }
80
81 return $options;
82 }
83
84 /**
85 * Returns the short links from permalink
86 *
87 * @param string|integer $page_id - the page id.
88 * @return string - the permalink.
89 */
90 public function gen_short_link_from_permalink( $page_id ) {
91 $permalink = get_permalink( $page_id );
92 $permalink = str_replace( site_url( '/' ), '', $permalink );
93
94 if ( substr( $permalink, - 1 ) === '/' ) {
95 $permalink = substr_replace( $permalink, '', - 1 );
96 }
97
98 return $permalink;
99 }
100
101 /**
102 * Returns slug
103 *
104 * @param string $title - Title of the short link.
105 * @return string - hyphenized slug
106 */
107 public function gen_slug_from_title( $title ) {
108 $string = strtolower( $title );
109 $string = preg_replace( '/-+/', '', $string );
110 $string = preg_replace( '/\s+/', '-', $string );
111 $string = preg_replace( '/[^a-z0-9-]/', '', $string );
112
113 return $string;
114 }
115
116 /**
117 * Description
118 *
119 * @param Any $data - Data.
120 */
121 public function disable_elementor_preview_redirect( $data ) {
122 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- detecting Elementor preview mode, no state mutation.
123 $elementor_preview = isset( $_GET['elementor-preview'] );
124
125 if ( $elementor_preview ) {
126 return false;
127 }
128
129 return $data;
130 }
131
132 /**
133 * Instant redirect controls
134 *
135 * @param Object $controls - Controls.
136 */
137 public function instant_redirect_controls( $controls ) {
138 $controls->start_controls_section(
139 'bl_instant_redirect_section',
140 array(
141 'label' => sprintf( '<i class="btl btl-logo"></i> %s', __( 'BetterLinks Instant Redirect', 'betterlinks' ) ),
142 'tab' => Controls_Manager::TAB_SETTINGS,
143 )
144 );
145
146 $controls->add_control(
147 'bl_ir_active',
148 array(
149 'label' => esc_html__( 'Enable Instant Redirect', 'betterlinks' ),
150 'type' => Controls_Manager::SWITCHER,
151 'label_on' => esc_html__( 'On', 'betterlinks' ),
152 'label_off' => esc_html__( 'Off', 'betterlinks' ),
153 'return_value' => 'yes',
154 'default' => '',
155 )
156 );
157
158 $controls->add_control(
159 'bl_ir_target_url',
160 array(
161 'type' => Controls_Manager::TEXT,
162 'label' => __( 'Target URL', 'betterlinks' ),
163 'condition' => array(
164 'bl_ir_active' => 'yes',
165 ),
166 )
167 );
168
169 $controls->add_control(
170 'bl_ir_redirect_type',
171 array(
172 'type' => Controls_Manager::SELECT,
173 'label' => __( 'Redirect Type', 'betterlinks' ),
174 'default' => $this->bl_get_link_options( 'redirect_type' ),
175 'options' => array(
176 '307' => esc_html__( '307 (Temporary)', 'betterlinks' ),
177 '302' => esc_html__( '302 (Temporary)', 'betterlinks' ),
178 '301' => esc_html__( '301 (Permanent)', 'betterlinks' ),
179 'cloak' => esc_html__( 'Cloaked', 'betterlinks' ),
180 ),
181 'condition' => array(
182 'bl_ir_active' => 'yes',
183 ),
184 )
185 );
186
187 $controls->add_control(
188 'bl_ir_link_category',
189 array(
190 'type' => Controls_Manager::SELECT,
191 'label' => __( 'Choose Category', 'betterlinks' ),
192 'default' => $this->bl_get_category_options( true ),
193 'options' => $this->bl_get_category_options(),
194 'condition' => array(
195 'bl_ir_active' => 'yes',
196 ),
197 )
198 );
199
200 $controls->add_control(
201 'bl_ir_link_options_heading',
202 array(
203 'type' => Controls_Manager::HEADING,
204 'label' => __( 'Link Options', 'betterlinks' ),
205 'condition' => array(
206 'bl_ir_active' => 'yes',
207 ),
208 'separator' => 'before',
209 )
210 );
211
212 $controls->add_control(
213 'bl_ir_link_options_nofollow',
214 array(
215 'label' => esc_html__( 'No Follow', 'betterlinks' ),
216 'type' => Controls_Manager::SWITCHER,
217 'label_on' => esc_html__( 'On', 'betterlinks' ),
218 'label_off' => esc_html__( 'Off', 'betterlinks' ),
219 'return_value' => 'yes',
220 'default' => $this->bl_get_link_options( 'nofollow' ) === true ? 'yes' : '',
221 'condition' => array(
222 'bl_ir_active' => 'yes',
223 ),
224 )
225 );
226
227 $controls->add_control(
228 'bl_ir_link_options_sponsored',
229 array(
230 'label' => esc_html__( 'Sponsored', 'betterlinks' ),
231 'type' => Controls_Manager::SWITCHER,
232 'label_on' => esc_html__( 'On', 'betterlinks' ),
233 'label_off' => esc_html__( 'Off', 'betterlinks' ),
234 'return_value' => 'yes',
235 'default' => $this->bl_get_link_options( 'sponsored' ) === true ? 'yes' : '',
236 'condition' => array(
237 'bl_ir_active' => 'yes',
238 ),
239 )
240 );
241
242 $controls->add_control(
243 'bl_ir_link_options_parameter_forwarding',
244 array(
245 'label' => esc_html__( 'Parameter Forwarding', 'betterlinks' ),
246 'type' => Controls_Manager::SWITCHER,
247 'label_on' => esc_html__( 'On', 'betterlinks' ),
248 'label_off' => esc_html__( 'Off', 'betterlinks' ),
249 'return_value' => 'yes',
250 'default' => $this->bl_get_link_options( 'param_forwarding' ) === true ? 'yes' : '',
251 'condition' => array(
252 'bl_ir_active' => 'yes',
253 ),
254 )
255 );
256
257 $controls->add_control(
258 'bl_ir_link_options_tracking',
259 array(
260 'label' => esc_html__( 'Tracking', 'betterlinks' ),
261 'type' => Controls_Manager::SWITCHER,
262 'label_on' => esc_html__( 'On', 'betterlinks' ),
263 'label_off' => esc_html__( 'Off', 'betterlinks' ),
264 'return_value' => 'yes',
265 'default' => $this->bl_get_link_options( 'track_me' ) === true ? 'yes' : '',
266 'condition' => array(
267 'bl_ir_active' => 'yes',
268 ),
269 )
270 );
271
272 if ( ! class_exists( 'BetterLinksPro' ) ) {
273 $controls->add_control(
274 'bl_ir_adv_protext',
275 array(
276 'type' => Controls_Manager::RAW_HTML,
277 'condition' => array(
278 'bl_ir_active' => 'yes',
279 ),
280 /* translators: %s: URL to the BetterLinks Pro upgrade page. */
281 'raw' => sprintf( __( 'Get the <a href="%s" target="_blank" style="color: red;">PRO</a> version for more advanced link redirection features & many more!', 'betterlinks' ), 'https://wpdeveloper.com/in/upgrade-betterlinks' ),
282 )
283 );
284 }
285
286 do_action( 'betterlinks/elementor/controllers/before-end', $controls );
287
288 $controls->end_controls_section();
289 }
290
291 /**
292 * Handle instant redirect data
293 *
294 * @param string|integer $post_id - ID of the post.
295 */
296 public function handle_instant_redirect_data( $post_id ) {
297 if ( wp_doing_cron() || get_post_type( $post_id ) === 'revision' ) {
298 return;
299 }
300
301 $document = Plugin::$instance->documents->get( $post_id, false );
302 $current_time = current_time( 'U' );
303 $current_gmt_time = time();
304 $title = $document->get_settings( 'post_title' );
305 $shortLink = $this->gen_short_link_from_permalink( $post_id );
306 $link = Helper::get_link_by_short_url( $shortLink );
307 $link_id = isset( $link[0]['ID'] ) ? $link[0]['ID'] : 'undefined';
308 $is_active = $document->get_settings( 'bl_ir_active' );
309 $redirect_type = $document->get_settings( 'bl_ir_redirect_type' );
310 $instant_redirect_data = array(
311 'ID' => $link_id,
312 'target_url' => $document->get_settings( 'bl_ir_target_url' ),
313 'cat_id' => $document->get_settings( 'bl_ir_link_category' ),
314 'redirect_type' => ! empty( $redirect_type ) ? $redirect_type : '307',
315 'nofollow' => $document->get_settings( 'bl_ir_link_options_nofollow' ) === 'yes' ? 1 : '',
316 'param_forwarding' => $document->get_settings( 'bl_ir_link_options_parameter_forwarding' ) === 'yes' ? 1 : '',
317 'sponsored' => $document->get_settings( 'bl_ir_link_options_sponsored' ) === 'yes' ? 1 : '',
318 'track_me' => $document->get_settings( 'bl_ir_link_options_tracking' ) === 'yes' ? 1 : '',
319 'link_slug' => $this->gen_slug_from_title( $title ),
320 'link_title' => $title,
321 'short_url' => $shortLink,
322 'link_date' => gmdate( 'Y-m-d H:i:s', $current_time ),
323 'link_date_gmt' => gmdate( 'Y-m-d H:i:s', $current_gmt_time ),
324 'link_modified' => gmdate( 'Y-m-d H:i:s', $current_time ),
325 'link_modified_gmt' => gmdate( 'Y-m-d H:i:s', $current_gmt_time ),
326 );
327
328 $instant_redirect_data = apply_filters( 'betterlinks/elementor/editor/after_save_data', $instant_redirect_data, $document );
329
330 if ( empty( $instant_redirect_data['target_url'] ) ) {
331 return;
332 }
333
334 if ( 'undefined' === $link_id && 'yes' === $is_active ) {
335 $args = $this->sanitize_links_data( $instant_redirect_data );
336 $this->insert_link( $args );
337 } elseif ( 'undefined' !== $link_id && 'yes' === $is_active ) {
338 $args = $this->sanitize_links_data( $instant_redirect_data );
339 unset( $args['link_date'], $args['link_date_gmt'] );
340 $this->update_link( $args );
341 } elseif ( 'undefined' !== $link_id && 'yes' !== $is_active ) {
342 $args = array(
343 'ID' => sanitize_text_field( $link_id ),
344 'short_url' => sanitize_text_field( $shortLink ),
345 );
346 $this->delete_link( $args );
347 Helper::delete_link_meta( $args['ID'], 'keywords' );
348 }
349 }
350
351 /**
352 * Handle elementor page settings data
353 *
354 * @param array|mixed $data - page data.
355 */
356 public function handle_page_data( $data ) {
357 if ( empty( $data['settings']['bl_ir_target_url'] ) || filter_var( $data['settings']['bl_ir_target_url'], FILTER_VALIDATE_URL ) === false ) {
358 $data['settings']['bl_ir_active'] = '';
359 }
360 return $data;
361 }
362 }
363