PluginProbe
Lasso Lite – Affiliate Link Manager & Product Displays / 158
Lasso Lite – Affiliate Link Manager & Product Displays v158
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 158, at includes/class-simple-urls.php

311 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 $get = Helper::GET();
43
44 $rewrite_slug_default = 'go';
45
46 $labels = array(
47 'name' => __( 'Lasso Lite', 'simple-urls' ), // phpcs:ignore
48 'singular_name' => __( 'URL', 'simple-urls' ), // phpcs:ignore
49 'add_new' => __( 'Add New', 'simple-urls' ),
50 'add_new_item' => __( 'Add New URL', 'simple-urls' ),
51 'edit' => __( 'Edit', 'simple-urls' ),
52 'edit_item' => __( 'Edit URL', 'simple-urls' ),
53 'new_item' => __( 'New URL', 'simple-urls' ),
54 'view' => __( 'View URL', 'simple-urls' ),
55 'view_item' => __( 'View URL', 'simple-urls' ),
56 'search_items' => __( 'Search URL', 'simple-urls' ),
57 'not_found' => __( 'No URLs found', 'simple-urls' ),
58 'not_found_in_trash' => __( 'No URLs found in Trash', 'simple-urls' ),
59 'messages' => array(
60 0 => '', // Unused. Messages start at index 1.
61 /* translators: %s: link for the update */
62 1 => __( 'URL updated. <a href="%s">View URL</a>', 'simple-urls' ),
63 2 => __( 'Custom field updated.', 'simple-urls' ),
64 3 => __( 'Custom field deleted.', 'simple-urls' ),
65 4 => __( 'URL updated.', 'simple-urls' ),
66 /* translators: %s: date and time of the revision */
67 5 => isset( $get['revision'] ) ? sprintf( __( 'Post restored to revision from %s', 'simple-urls' ), wp_post_revision_title( (int) $get['revision'], false ) ) : false, // phpcs:ignore
68 /* translators: %s: URL to view */
69 6 => __( 'URL updated. <a href="%s">View URL</a>', 'simple-urls' ),
70 7 => __( 'URL saved.', 'simple-urls' ),
71 8 => __( 'URL submitted.', 'simple-urls' ),
72 9 => __( 'URL scheduled', 'simple-urls' ),
73 10 => __( 'URL draft updated.', 'simple-urls' ),
74 ),
75 );
76
77 $labels = apply_filters( 'simple_urls_cpt_labels', $labels );
78
79 $rewrite_slug = apply_filters( 'simple_urls_slug', $rewrite_slug_default );
80
81 $rewrite_slug = sanitize_title( $rewrite_slug, $rewrite_slug_default );
82
83 // Ref: https://developer.wordpress.org/reference/functions/add_post_type_support/.
84 $supports_array = apply_filters( 'simple_urls_post_type_supports', array( 'title' ) );
85
86 // Ref: https://developer.wordpress.org/reference/functions/register_post_type/.
87 register_post_type(
88 $slug,
89 array(
90 'labels' => $labels,
91 'public' => true,
92 'exclude_from_search' => apply_filters( 'simple_urls_exclude_from_search', true ),
93 'show_ui' => true,
94 'query_var' => true,
95 'menu_position' => 20,
96 'supports' => $supports_array,
97 'rewrite' => array(
98 'slug' => $rewrite_slug,
99 'with_front' => false,
100 ),
101 'show_in_rest' => true,
102 // WARNING | base64_encode() can be used to obfuscate code which is strongly discouraged.
103 // Please verify that the function is used for benign reasons.
104 // phpcs:ignore
105 'menu_icon' => 'data:image/svg+xml;base64,' . base64_encode( $menu_icon ),
106 )
107 );
108
109 if ( ! $this->is_rewrite_url_added() ) {
110 flush_rewrite_rules();
111 }
112
113 }
114
115 /**
116 * Count and redirect function.
117 */
118 public function count_and_redirect() {
119
120 $redirect_url = wp_unslash( $_SERVER['REDIRECT_URL'] ?? '' ); // phpcs:ignore
121 $link = ! empty( $redirect_url ) ? $redirect_url : $_SERVER['QUERY_STRING'] ?? ''; // phpcs:ignore
122
123 if ( strpos( $link, Enum::SLUG_CLOAK_FOLLOW_TWITTER ) !== false ) {
124 update_option( Enum::FOLLOW_ON_TWITTER, true );
125 wp_redirect( Enum::TWITTER_URL, 302 ); // phpcs:ignore
126 }
127
128 if ( strpos( $link, Enum::SLUG_CLOAK_SHARE_TWITTER ) !== false ) {
129 update_option( Enum::SHARE_ON_TWITTER, true );
130 wp_redirect( Enum::TWITTER_SHARE_URL, 302 ); // phpcs:ignore
131 }
132
133 if ( strpos( $link, Enum::SLUG_CLOAK_LASSO_REVIEW_URL ) !== false ) {
134 update_option( Enum::LEAVE_A_REVIEW, true );
135 wp_redirect( Enum::LASSO_REVIEW_URL, 302 ); // phpcs:ignore
136 }
137 if ( ! is_singular( 'surl' ) ) {
138 return;
139 }
140
141 global $wp_query;
142
143 // Update the count.
144 $count = isset( $wp_query->post->_surl_count ) ? (int) $wp_query->post->_surl_count : 0;
145 update_post_meta( $wp_query->post->ID, '_surl_count', $count + 1 );
146
147 // Handle the redirect.
148 $redirect = isset( $wp_query->post->ID ) ? get_post_meta( $wp_query->post->ID, '_surl_redirect', true ) : '';
149
150 /**
151 * Filter the redirect URL.
152 *
153 * @since 0.9.5
154 *
155 * @param string $redirect The URL to redirect to.
156 * @param int $var The current click count.
157 */
158 $redirect = apply_filters( 'simple_urls_redirect_url', $redirect, $count );
159
160 /**
161 * Action hook that fires before the redirect.
162 *
163 * @since 0.9.5
164 *
165 * @param string $redirect The URL to redirect to.
166 * @param int $var The current click count.
167 */
168 do_action( 'simple_urls_redirect', $redirect, $count );
169
170 if ( ! empty( $redirect ) ) {
171 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.
172 exit;
173 } else {
174 wp_safe_redirect( home_url(), 302 );
175 exit;
176 }
177
178 }
179
180 /**
181 * Use Amazon product url instead of cloaked url when search/insert a link into a post/page
182 *
183 * @param array $results An array of associative arrays of query results.
184 * @return array $results Full wp_link_query array of query results with updated permalinks.
185 */
186 public function use_amazon_url_instead_of_cloaked( $results ) {
187 foreach ( $results as $key => $post ) {
188 $post_id = isset( $post['ID'] ) ? $post['ID'] : null;
189 $redirect = isset( $post_id ) ? get_post_meta( $post_id, '_surl_redirect', true ) : '';
190 $permalink = $post['permalink'] ?? '';
191
192 if ( self::is_amazon_url( $redirect ) ) {
193 // Set Amazon product url.
194 $results[ $key ]['permalink'] = $redirect;
195 }
196 }
197
198 return $results;
199 }
200
201 /**
202 * Get amazon domains
203 *
204 * @return array All of the valid Amazon domains.
205 */
206 public static function get_domains() {
207 return array(
208 'amazon.com', // US.
209 'amazon.ca', // Canada.
210 'amazon.co.uk', // UK.
211 'amazon.com.au', // Australia.
212 'amazon.com.br', // Brazil.
213 'amazon.com.mx', // Mexico.
214 'amazon.fr', // France.
215 'amazon.de', // Germany.
216 'amazon.it', // Italy.
217 'amazon.in', // India.
218 'amazon.es', // Spain.
219 'amazon.cn', // China.
220 'amazon.co.jp', // Japan.
221 'amazon.nl', // Netherlands.
222 'amazon.se', // Sweden.
223 'amazon.sg', // Singapore.
224 'amazon.com.tr', // Turkey.
225 'amazon.ae', // United Arab Emirates.
226 'amzn.com', // Short URL.
227 'amzn.to', // Short URL.
228 'amazon-adsystem.com', // Amazon Embed.
229 'smile.amazon.com', // Amazon Smile.
230 );
231 }
232
233 /**
234 * Check whether a url is amazon link or not
235 *
236 * @param string $url Url.
237 * @return bool If a url is a valid amazon url or not.
238 */
239 public static function is_amazon_url( $url ) {
240 if ( empty( $url ) ) {
241 return false;
242 }
243
244 $domains = self::get_domains();
245 $url = self::add_https( $url );
246
247 if ( ! self::validate_url( $url ) ) {
248 return false;
249 }
250
251 $parsed_url = wp_parse_url( $url );
252 if ( ! isset( $parsed_url['host'] ) ) {
253 return false;
254 }
255
256 $domain = ltrim( $parsed_url['host'], 'www.' );
257
258 if ( in_array( $domain, $domains, true ) ) {
259 return true;
260 }
261
262 return false;
263 }
264
265 /**
266 * Add https to the url
267 *
268 * @param string $url URL.
269 * @return string $url URL with https
270 */
271 public static function add_https( $url ) {
272 $url = trim( $url );
273 if ( '' === $url || is_null( $url ) ) {
274 return $url;
275 }
276
277 return preg_replace( '#^http(?=://)#', 'https', $url );
278 }
279
280 /**
281 * Validate URL
282 *
283 * @param string $url URL.
284 * @return bool if the url submitted is valid or not
285 */
286 public static function validate_url( $url ) {
287 return ( ( strpos( $url, 'http://' ) === 0 || strpos( $url, 'https://' ) === 0 ) && filter_var( $url, FILTER_VALIDATE_URL ) !== false );
288 }
289
290 /**
291 * Check the plugin is already added rewrite url
292 *
293 * @return bool
294 */
295 private function is_rewrite_url_added() {
296 $is_added = false;
297 $rewrite_rules = get_option( 'rewrite_rules' );
298 if ( ! empty( $rewrite_rules ) ) {
299 foreach ( $rewrite_rules as $rewrite_key => $rewrite_rule ) {
300 if ( strpos( $rewrite_key, 'go/[^/]' ) !== false ) {
301 $is_added = true;
302 break;
303 }
304 }
305 }
306
307 return $is_added;
308 }
309
310 }
311