class-external-permalinks.php
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | /** |
| 6 | * Class for External Permalinks module |
| 7 | * |
| 8 | * @since 6.9.5 |
| 9 | */ |
| 10 | class External_Permalinks { |
| 11 | /** |
| 12 | * Check if External Permalinks is enabled for the given post type. |
| 13 | * |
| 14 | * In the free version, this always behaves like 'only-on'. In the pro |
| 15 | * version, this can be 'only-on', 'except-on' or 'all-post-types'. |
| 16 | * |
| 17 | * @param string $post_type_slug Post type slug. |
| 18 | * @param array $options Plugin options. |
| 19 | * @return bool True when enabled for the post type. |
| 20 | */ |
| 21 | private function is_enabled_for_post_type( $post_type_slug, $options = array() ) { |
| 22 | if ( empty( $post_type_slug ) ) { |
| 23 | return false; |
| 24 | } |
| 25 | $enabled_for = ( isset( $options['enable_external_permalinks_for'] ) && is_array( $options['enable_external_permalinks_for'] ) ? $options['enable_external_permalinks_for'] : array() ); |
| 26 | // Only operate on applicable post types (public, excluding attachment). |
| 27 | $applicable_post_types = array_keys( $enabled_for ); |
| 28 | $applicable_post_types = array_values( array_diff( $applicable_post_types, array('attachment') ) ); |
| 29 | if ( empty( $applicable_post_types ) || !in_array( $post_type_slug, $applicable_post_types, true ) ) { |
| 30 | return false; |
| 31 | } |
| 32 | $type = 'only-on'; |
| 33 | $selected_post_types = array(); |
| 34 | foreach ( $enabled_for as $slug => $is_enabled ) { |
| 35 | if ( 'attachment' === $slug ) { |
| 36 | continue; |
| 37 | } |
| 38 | if ( $is_enabled ) { |
| 39 | $selected_post_types[] = $slug; |
| 40 | } |
| 41 | } |
| 42 | // Default to 'only-on'. |
| 43 | return in_array( $post_type_slug, $selected_post_types, true ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Check whether an external permalink should open in a new tab. |
| 48 | * |
| 49 | * In the pro version, reads the Page Links To-compatible _links_to_target meta. |
| 50 | * |
| 51 | * @param int $post_id Post ID. |
| 52 | * @return bool True when the link should open in a new tab. |
| 53 | */ |
| 54 | private function should_open_in_new_tab( $post_id ) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Add external permalink meta box for enabled post types |
| 60 | * |
| 61 | * @since 3.9.0 |
| 62 | */ |
| 63 | public function add_external_permalink_meta_box( $post_type, $post ) { |
| 64 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 65 | if ( !$this->is_enabled_for_post_type( $post_type, $options ) ) { |
| 66 | return; |
| 67 | } |
| 68 | // Skip adding meta box for post types where Gutenberg is enabled |
| 69 | // if ( |
| 70 | // function_exists( 'use_block_editor_for_post_type' ) |
| 71 | // && use_block_editor_for_post_type( $post_type ) |
| 72 | // ) { |
| 73 | // return; |
| 74 | // } |
| 75 | add_meta_box( |
| 76 | 'asenha-external-permalink', |
| 77 | // ID of meta box |
| 78 | 'External Permalink', |
| 79 | // Title of meta box |
| 80 | [$this, 'output_external_permalink_meta_box'], |
| 81 | // Callback function |
| 82 | $post_type, |
| 83 | // The screen on which the meta box should be output to |
| 84 | 'normal', |
| 85 | // context |
| 86 | 'high' |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Render External Permalink meta box |
| 92 | * |
| 93 | * @since 3.9.0 |
| 94 | */ |
| 95 | public function output_external_permalink_meta_box( $post ) { |
| 96 | ?> |
| 97 | <div class="external-permalink-input"> |
| 98 | <input name="<?php |
| 99 | echo esc_attr( 'external_permalink' ); |
| 100 | ?>" class="large-text" id="<?php |
| 101 | echo esc_attr( 'external_permalink' ); |
| 102 | ?>" type="text" value="<?php |
| 103 | echo esc_url( get_post_meta( $post->ID, '_links_to', true ) ); |
| 104 | ?>" placeholder="https://" /> |
| 105 | <div class="external-permalink-input-description"><?php |
| 106 | esc_html_e( 'Keep empty to use the default WordPress permalink.', 'admin-site-enhancements' ); |
| 107 | ?></div> |
| 108 | <?php |
| 109 | ?> |
| 110 | <?php |
| 111 | wp_nonce_field( |
| 112 | 'external_permalink_' . $post->ID, |
| 113 | 'external_permalink_nonce', |
| 114 | false, |
| 115 | true |
| 116 | ); |
| 117 | ?> |
| 118 | </div> |
| 119 | <?php |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Save external permalink input |
| 124 | * |
| 125 | * @since 3.9.0 |
| 126 | */ |
| 127 | public function save_external_permalink( $post_id ) { |
| 128 | // Only proceed if nonce is verified |
| 129 | if ( isset( $_POST['external_permalink_nonce'] ) && wp_verify_nonce( $_POST['external_permalink_nonce'], 'external_permalink_' . $post_id ) ) { |
| 130 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 131 | $post_type = get_post_type( $post_id ); |
| 132 | if ( !$this->is_enabled_for_post_type( $post_type, $options ) ) { |
| 133 | return; |
| 134 | } |
| 135 | // Get the value of external permalink from input field |
| 136 | $external_permalink = ( isset( $_POST['external_permalink'] ) ? esc_url_raw( trim( $_POST['external_permalink'] ) ) : '' ); |
| 137 | // Update or delete external permalink post meta |
| 138 | if ( !empty( $external_permalink ) ) { |
| 139 | update_post_meta( $post_id, '_links_to', $external_permalink ); |
| 140 | } else { |
| 141 | delete_post_meta( $post_id, '_links_to' ); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Change WordPress default permalink into external permalink for pages |
| 148 | * |
| 149 | * @since 3.9.0 |
| 150 | */ |
| 151 | public function use_external_permalink_for_pages( $permalink, $post_id ) { |
| 152 | $request_uri = sanitize_text_field( $_SERVER['REQUEST_URI'] ); |
| 153 | // e.g. /wp-admin/index.php?page=page-slug |
| 154 | if ( false === strpos( $request_uri, 'mfn-live-builder' ) ) { |
| 155 | // When not in BeTheme template builder, that has the 'action=mfn-live-builder' parameter in the URL |
| 156 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 157 | $post_type = get_post_type( $post_id ); |
| 158 | if ( !$this->is_enabled_for_post_type( $post_type, $options ) ) { |
| 159 | return $permalink; |
| 160 | } |
| 161 | $external_permalink = get_post_meta( $post_id, '_links_to', true ); |
| 162 | if ( !empty( $external_permalink ) ) { |
| 163 | $permalink = $external_permalink; |
| 164 | if ( !is_admin() ) { |
| 165 | $permalink = $permalink . '#asenha_ep'; |
| 166 | if ( $this->should_open_in_new_tab( $post_id ) ) { |
| 167 | $permalink = $permalink . '#new_tab'; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | return $permalink; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Change WordPress default permalink into external permalink for posts and custom post types |
| 177 | * |
| 178 | * @since 3.9.0 |
| 179 | */ |
| 180 | public function use_external_permalink_for_posts( $permalink, $post ) { |
| 181 | $request_uri = sanitize_text_field( $_SERVER['REQUEST_URI'] ); |
| 182 | // e.g. /wp-admin/index.php?page=page-slug |
| 183 | if ( false === strpos( $request_uri, 'mfn-live-builder' ) ) { |
| 184 | // When not in BeTheme template builder, that has the 'action=mfn-live-builder' parameter in the URL |
| 185 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 186 | $post_type = ( is_object( $post ) && isset( $post->post_type ) ? $post->post_type : '' ); |
| 187 | if ( !$this->is_enabled_for_post_type( $post_type, $options ) ) { |
| 188 | return $permalink; |
| 189 | } |
| 190 | $external_permalink = get_post_meta( $post->ID, '_links_to', true ); |
| 191 | if ( !empty( $external_permalink ) ) { |
| 192 | $permalink = $external_permalink; |
| 193 | if ( !is_admin() ) { |
| 194 | $permalink = $permalink . '#asenha_ep'; |
| 195 | if ( $this->should_open_in_new_tab( $post->ID ) ) { |
| 196 | $permalink = $permalink . '#new_tab'; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | return $permalink; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Redirect page/post to external permalink if it's loaded directly from the WP default permalink |
| 206 | * |
| 207 | * @since 3.9.0 |
| 208 | */ |
| 209 | public function redirect_to_external_permalink() { |
| 210 | // If not on/loading the single page/post URL, do nothing |
| 211 | if ( !is_singular() ) { |
| 212 | return; |
| 213 | } |
| 214 | global $post; |
| 215 | if ( !is_null( $post ) && is_object( $post ) && is_a( $post, 'WP_Post' ) ) { |
| 216 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 217 | if ( empty( $post->post_type ) || !$this->is_enabled_for_post_type( $post->post_type, $options ) ) { |
| 218 | return; |
| 219 | } |
| 220 | if ( property_exists( $post, 'ID' ) ) { |
| 221 | $external_permalink = get_post_meta( $post->ID, '_links_to', true ); |
| 222 | if ( !empty( $external_permalink ) ) { |
| 223 | wp_redirect( $external_permalink, 302 ); |
| 224 | // temporary redirect |
| 225 | exit; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Build frontend inline script config from plugin options. |
| 233 | * |
| 234 | * @since 8.8.0 |
| 235 | * |
| 236 | * @param array $options Plugin options. |
| 237 | * @return array |
| 238 | */ |
| 239 | private function get_frontend_script_config( array $options ) { |
| 240 | $config = array( |
| 241 | 'excludeNofollowDomains' => array(), |
| 242 | ); |
| 243 | return $config; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Return inline frontend script for external permalink link processing. |
| 248 | * |
| 249 | * @since 8.8.0 |
| 250 | * |
| 251 | * @param array $config Script config from get_frontend_script_config(). |
| 252 | * @return string |
| 253 | */ |
| 254 | private function get_frontend_inline_script( array $config ) { |
| 255 | $js = <<<'JS' |
| 256 | (function () { |
| 257 | 'use strict'; |
| 258 | |
| 259 | document.addEventListener( 'DOMContentLoaded', function () { |
| 260 | |
| 261 | var links = document.getElementsByTagName( 'a' ); |
| 262 | |
| 263 | for ( var i = 0; i < links.length; i++ ) { |
| 264 | var url = links[i].getAttribute( 'href' ); |
| 265 | |
| 266 | if ( url == null ) { |
| 267 | continue; |
| 268 | } |
| 269 | |
| 270 | var hasAsenhaEp = url.indexOf( '#asenha_ep' ) >= 0; |
| 271 | |
| 272 | if ( ! hasAsenhaEp ) { |
| 273 | continue; |
| 274 | } |
| 275 | |
| 276 | url = url.replace( '#asenha_ep', '' ); |
| 277 | links[i].setAttribute( 'href', url ); |
| 278 | links[i].setAttribute( 'rel', 'noopener noreferrer nofollow' ); |
| 279 | } |
| 280 | |
| 281 | } ); |
| 282 | |
| 283 | } ) |
| 284 | JS; |
| 285 | return $js . '();'; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Return inline frontend script built from plugin options. |
| 290 | * |
| 291 | * @since 8.8.0 |
| 292 | * |
| 293 | * @param array $options Plugin options. |
| 294 | * @return string |
| 295 | */ |
| 296 | public function get_frontend_inline_script_for_options( array $options ) { |
| 297 | return $this->get_frontend_inline_script( $this->get_frontend_script_config( $options ) ); |
| 298 | } |
| 299 | |
| 300 | } |
| 301 |