PluginProbe
Lasso Lite – Affiliate Link Manager & Product Displays / 111
Lasso Lite – Affiliate Link Manager & Product Displays v111
158 157 155 156 154 153 152 151 150 149 148 trunk 0.9.9 104 105 106 107 108 109 110 111 112 113 114 115 All 57 releases
simple-urls / includes / class-simple-urls.php

class-simple-urls.php in Lasso Lite – Affiliate Link Manager & Product Displays 111, at includes/class-simple-urls.php

310 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Simple URLs file.
4 *
5 * @package simple-urls
6 */
7
8 use LassoLite\Classes\Enum;
9 use LassoLite\Classes\Helper;
10
11 /**
12 * Simple URLs class.
13 */
14 class Simple_Urls {
15
16 /**
17 * Constructor.
18 */
19 public function __construct() {
20
21 add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
22 add_action( 'init', array( $this, 'register_post_type' ) );
23 add_action( 'template_redirect', array( $this, 'count_and_redirect' ) );
24 add_filter( 'wp_link_query', array( $this, 'use_amazon_url_instead_of_cloaked' ), 100 );
25
26 }
27
28 /**
29 * Load textdomain.
30 */
31 public function load_textdomain() {
32 load_plugin_textdomain( 'simple-urls', false, SIMPLE_URLS_DIR . '/languages' );
33 }
34
35 /**
36 * Register Post Type.
37 */
38 public function register_post_type() {
39 $menu_icon = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="20" height="20" viewBox="0 0 100 100"><defs><clipPath id="b"><rect width="100" height="100"/></clipPath></defs><g id="a" clip-path="url(#b)"><g transform="translate(-72.918 -0.438)"><g transform="translate(90.918 0.437)"><path d="M60.056,20.152h0a8.093,8.093,0,0,0-8.244,8.172l.141,3.177c-.118,19.814-5.749,18.873-6.665,18.613V13.183A8.353,8.353,0,0,0,36.819,5h0a8.356,8.356,0,0,0-8.472,8.183V50.541c-7.743,2.007-6.385-20.85-6.385-20.85V21.736c0-4.586-4.435-8.292-9.19-8.292h0c-4.745,0-7.75,3.752-7.75,8.338l-.023,8C7.3,65.234,28.347,62,28.347,62l.011,11.943H45.3L45.29,61.962c21.954-.087,23.324-30.387,23.324-30.387V28.419A8.443,8.443,0,0,0,60.056,20.152Z" transform="translate(-5 -5)" fill="#fff"/><path d="M1.519,12.038c0,3.246,1.971,5.9,4.382,5.9H20.077c2.411,0,4.384-2.656,4.384-5.9L25.975,0H0Z" transform="translate(19.053 82.06)" fill="#fff"/></g><path d="M127.888,425.8H98.6a2.75,2.75,0,0,0-2.424,2.974v4.687a2.75,2.75,0,0,0,2.424,2.974h29.283a2.751,2.751,0,0,0,2.428-2.974v-4.687A2.751,2.751,0,0,0,127.888,425.8Z" transform="translate(9.715 -355.14)" fill="#fff"/></g></g></svg>';
40
41 $slug = 'surl';
42
43 $rewrite_slug_default = 'go';
44
45 $labels = array(
46 'name' => __( 'Lasso Lite / Simple URLs', 'simple-urls' ), // phpcs:ignore
47 'singular_name' => __( 'URL', 'simple-urls' ), // phpcs:ignore
48 'add_new' => __( 'Add New', 'simple-urls' ),
49 'add_new_item' => __( 'Add New URL', 'simple-urls' ),
50 'edit' => __( 'Edit', 'simple-urls' ),
51 'edit_item' => __( 'Edit URL', 'simple-urls' ),
52 'new_item' => __( 'New URL', 'simple-urls' ),
53 'view' => __( 'View URL', 'simple-urls' ),
54 'view_item' => __( 'View URL', 'simple-urls' ),
55 'search_items' => __( 'Search URL', 'simple-urls' ),
56 'not_found' => __( 'No URLs found', 'simple-urls' ),
57 'not_found_in_trash' => __( 'No URLs found in Trash', 'simple-urls' ),
58 'messages' => array(
59 0 => '', // Unused. Messages start at index 1.
60 /* translators: %s: link for the update */
61 1 => __( 'URL updated. <a href="%s">View URL</a>', 'simple-urls' ),
62 2 => __( 'Custom field updated.', 'simple-urls' ),
63 3 => __( 'Custom field deleted.', 'simple-urls' ),
64 4 => __( 'URL updated.', 'simple-urls' ),
65 /* translators: %s: date and time of the revision */
66 5 => isset( $_GET['revision'] ) ? sprintf( __( 'Post restored to revision from %s', 'simple-urls' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, // phpcs:ignore
67 /* translators: %s: URL to view */
68 6 => __( 'URL updated. <a href="%s">View URL</a>', 'simple-urls' ),
69 7 => __( 'URL saved.', 'simple-urls' ),
70 8 => __( 'URL submitted.', 'simple-urls' ),
71 9 => __( 'URL scheduled', 'simple-urls' ),
72 10 => __( 'URL draft updated.', 'simple-urls' ),
73 ),
74 );
75
76 $labels = apply_filters( 'simple_urls_cpt_labels', $labels );
77
78 $rewrite_slug = apply_filters( 'simple_urls_slug', $rewrite_slug_default );
79
80 $rewrite_slug = sanitize_title( $rewrite_slug, $rewrite_slug_default );
81
82 // Ref: https://developer.wordpress.org/reference/functions/add_post_type_support/.
83 $supports_array = apply_filters( 'simple_urls_post_type_supports', array( 'title' ) );
84
85 // Ref: https://developer.wordpress.org/reference/functions/register_post_type/.
86 register_post_type(
87 $slug,
88 array(
89 'labels' => $labels,
90 'public' => true,
91 'exclude_from_search' => apply_filters( 'simple_urls_exclude_from_search', true ),
92 'show_ui' => true,
93 'query_var' => true,
94 'menu_position' => 20,
95 'supports' => $supports_array,
96 'rewrite' => array(
97 'slug' => $rewrite_slug,
98 'with_front' => false,
99 ),
100 'show_in_rest' => true,
101 // WARNING | base64_encode() can be used to obfuscate code which is strongly discouraged.
102 // Please verify that the function is used for benign reasons.
103 // phpcs:ignore
104 'menu_icon' => 'data:image/svg+xml;base64,' . base64_encode( $menu_icon ),
105 )
106 );
107
108 if ( ! $this->is_rewrite_url_added() ) {
109 flush_rewrite_rules();
110 }
111
112 }
113
114 /**
115 * Count and redirect function.
116 */
117 public function count_and_redirect() {
118
119 $redirect_url = wp_unslash( $_SERVER['REDIRECT_URL'] ?? '' ); // phpcs:ignore
120 $link = ! empty( $redirect_url ) ? $redirect_url : $_SERVER['QUERY_STRING'] ?? ''; // phpcs:ignore
121
122 if ( strpos( $link, Enum::SLUG_CLOAK_FOLLOW_TWITTER ) !== false ) {
123 update_option( Enum::FOLLOW_ON_TWITTER, true );
124 wp_redirect( Enum::TWITTER_URL, 302 ); // phpcs:ignore
125 }
126
127 if ( strpos( $link, Enum::SLUG_CLOAK_SHARE_TWITTER ) !== false ) {
128 update_option( Enum::SHARE_ON_TWITTER, true );
129 wp_redirect( Enum::TWITTER_SHARE_URL, 302 ); // phpcs:ignore
130 }
131
132 if ( strpos( $link, Enum::SLUG_CLOAK_LASSO_REVIEW_URL ) !== false ) {
133 update_option( Enum::LEAVE_A_REVIEW, true );
134 wp_redirect( Enum::LASSO_REVIEW_URL, 302 ); // phpcs:ignore
135 }
136 if ( ! is_singular( 'surl' ) ) {
137 return;
138 }
139
140 global $wp_query;
141
142 // Update the count.
143 $count = isset( $wp_query->post->_surl_count ) ? (int) $wp_query->post->_surl_count : 0;
144 update_post_meta( $wp_query->post->ID, '_surl_count', $count + 1 );
145
146 // Handle the redirect.
147 $redirect = isset( $wp_query->post->ID ) ? get_post_meta( $wp_query->post->ID, '_surl_redirect', true ) : '';
148
149 /**
150 * Filter the redirect URL.
151 *
152 * @since 0.9.5
153 *
154 * @param string $redirect The URL to redirect to.
155 * @param int $var The current click count.
156 */
157 $redirect = apply_filters( 'simple_urls_redirect_url', $redirect, $count );
158
159 /**
160 * Action hook that fires before the redirect.
161 *
162 * @since 0.9.5
163 *
164 * @param string $redirect The URL to redirect to.
165 * @param int $var The current click count.
166 */
167 do_action( 'simple_urls_redirect', $redirect, $count );
168
169 if ( ! empty( $redirect ) ) {
170 wp_redirect( esc_url_raw( $redirect ), 301 ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- the redirect URL was added by a user with access to the admin and is filterable. Adding to allowed_redirect_hosts does little to improve security here.
171 exit;
172 } else {
173 wp_safe_redirect( home_url(), 302 );
174 exit;
175 }
176
177 }
178
179 /**
180 * Use Amazon product url instead of cloaked url when search/insert a link into a post/page
181 *
182 * @param array $results An array of associative arrays of query results.
183 * @return array $results Full wp_link_query array of query results with updated permalinks.
184 */
185 public function use_amazon_url_instead_of_cloaked( $results ) {
186 foreach ( $results as $key => $post ) {
187 $post_id = isset( $post['ID'] ) ? $post['ID'] : null;
188 $redirect = isset( $post_id ) ? get_post_meta( $post_id, '_surl_redirect', true ) : '';
189 $permalink = $post['permalink'] ?? '';
190
191 if ( self::is_amazon_url( $redirect ) ) {
192 // Set Amazon product url.
193 $results[ $key ]['permalink'] = $redirect;
194 }
195 }
196
197 return $results;
198 }
199
200 /**
201 * Get amazon domains
202 *
203 * @return array All of the valid Amazon domains.
204 */
205 public static function get_domains() {
206 return array(
207 'amazon.com', // US.
208 'amazon.ca', // Canada.
209 'amazon.co.uk', // UK.
210 'amazon.com.au', // Australia.
211 'amazon.com.br', // Brazil.
212 'amazon.com.mx', // Mexico.
213 'amazon.fr', // France.
214 'amazon.de', // Germany.
215 'amazon.it', // Italy.
216 'amazon.in', // India.
217 'amazon.es', // Spain.
218 'amazon.cn', // China.
219 'amazon.co.jp', // Japan.
220 'amazon.nl', // Netherlands.
221 'amazon.se', // Sweden.
222 'amazon.sg', // Singapore.
223 'amazon.com.tr', // Turkey.
224 'amazon.ae', // United Arab Emirates.
225 'amzn.com', // Short URL.
226 'amzn.to', // Short URL.
227 'amazon-adsystem.com', // Amazon Embed.
228 'smile.amazon.com', // Amazon Smile.
229 );
230 }
231
232 /**
233 * Check whether a url is amazon link or not
234 *
235 * @param string $url Url.
236 * @return bool If a url is a valid amazon url or not.
237 */
238 public static function is_amazon_url( $url ) {
239 if ( empty( $url ) ) {
240 return false;
241 }
242
243 $domains = self::get_domains();
244 $url = self::add_https( $url );
245
246 if ( ! self::validate_url( $url ) ) {
247 return false;
248 }
249
250 $parsed_url = wp_parse_url( $url );
251 if ( ! isset( $parsed_url['host'] ) ) {
252 return false;
253 }
254
255 $domain = ltrim( $parsed_url['host'], 'www.' );
256
257 if ( in_array( $domain, $domains, true ) ) {
258 return true;
259 }
260
261 return false;
262 }
263
264 /**
265 * Add https to the url
266 *
267 * @param string $url URL.
268 * @return string $url URL with https
269 */
270 public static function add_https( $url ) {
271 $url = trim( $url );
272 if ( '' === $url || is_null( $url ) ) {
273 return $url;
274 }
275
276 return preg_replace( '#^http(?=://)#', 'https', $url );
277 }
278
279 /**
280 * Validate URL
281 *
282 * @param string $url URL.
283 * @return bool if the url submitted is valid or not
284 */
285 public static function validate_url( $url ) {
286 return ( ( strpos( $url, 'http://' ) === 0 || strpos( $url, 'https://' ) === 0 ) && filter_var( $url, FILTER_VALIDATE_URL ) !== false );
287 }
288
289 /**
290 * Check the plugin is already added rewrite url
291 *
292 * @return bool
293 */
294 private function is_rewrite_url_added() {
295 $is_added = false;
296 $rewrite_rules = get_option( 'rewrite_rules' );
297 if ( ! empty( $rewrite_rules ) ) {
298 foreach ( $rewrite_rules as $rewrite_key => $rewrite_rule ) {
299 if ( strpos( $rewrite_key, 'go/[^/]' ) !== false ) {
300 $is_added = true;
301 break;
302 }
303 }
304 }
305
306 return $is_added;
307 }
308
309 }
310