class-activation.php
1 year ago
class-admin-menu-organizer.php
1 year ago
class-auto-publish-posts-with-missed-schedule.php
1 year ago
class-avif-upload.php
1 year ago
class-captcha-protection.php
1 year ago
class-change-login-url.php
1 year ago
class-cleanup-admin-bar.php
1 year ago
class-common-methods.php
1 year ago
class-content-duplication.php
1 year ago
class-content-order.php
1 year ago
class-custom-admin-footer-text.php
1 year ago
class-custom-body-class.php
1 year ago
class-custom-css.php
1 year ago
class-custom-nav-menu-items-in-new-tab.php
1 year ago
class-deactivation.php
1 year ago
class-disable-comments.php
1 year ago
class-disable-dashboard-widgets.php
1 year ago
class-disable-feeds.php
1 year ago
class-disable-gutenberg.php
1 year ago
class-disable-rest-api.php
1 year ago
class-disable-smaller-components.php
1 year ago
class-disable-updates.php
1 year ago
class-disable-xml-rpc.php
1 year ago
class-display-system-summary.php
1 year ago
class-email-address-obfuscator.php
1 year ago
class-email-delivery.php
1 year ago
class-enhance-list-tables.php
1 year ago
class-external-permalinks.php
1 year ago
class-heartbeat-control.php
1 year ago
class-hide-admin-bar.php
1 year ago
class-hide-admin-notices.php
1 year ago
class-image-sizes-panel.php
1 year ago
class-image-upload-control.php
1 year ago
class-insert-head-body-footer-code.php
1 year ago
class-last-login-column.php
1 year ago
class-limit-login-attempts.php
1 year ago
class-login-id-type.php
1 year ago
class-login-logout-menu.php
1 year ago
class-maintenance-mode.php
1 year ago
class-manage-ads-appads-txt.php
1 year ago
class-manage-robots-txt.php
1 year ago
class-media-replacement.php
1 year ago
class-multiple-user-roles.php
1 year ago
class-obfuscate-author-slugs.php
1 year ago
class-open-external-links-in-new-tab.php
1 year ago
class-password-protection.php
1 year ago
class-redirect-after-login.php
1 year ago
class-redirect-after-logout.php
1 year ago
class-redirect-fourofour.php
1 year ago
class-registration-date-column.php
1 year ago
class-revisions-control.php
1 year ago
class-search-engines-visibility.php
1 year ago
class-settings-fields-render.php
1 year ago
class-settings-sanitization.php
1 year ago
class-settings-sections-fields.php
1 year ago
class-show-custom-taxonomy-filters.php
1 year ago
class-site-identity-on-login-page.php
1 year ago
class-svg-upload.php
1 year ago
class-various-admin-ui-enhancements.php
1 year ago
class-view-admin-as-role.php
1 year ago
class-wider-admin-menu.php
1 year ago
class-wp-config-transformer.php
1 year ago
class-external-permalinks.php
162 lines
| 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 | /** |
| 13 | * Add external permalink meta box for enabled post types |
| 14 | * |
| 15 | * @since 3.9.0 |
| 16 | */ |
| 17 | public function add_external_permalink_meta_box( $post_type, $post ) { |
| 18 | |
| 19 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 20 | $enable_external_permalinks_for = $options['enable_external_permalinks_for']; |
| 21 | |
| 22 | foreach ( $enable_external_permalinks_for as $post_type_slug => $is_external_permalink_enabled ) { |
| 23 | if ( ( get_post_type() == $post_type_slug ) && $is_external_permalink_enabled ) { |
| 24 | |
| 25 | // Skip adding meta box for post types where Gutenberg is enabled |
| 26 | // if ( |
| 27 | // function_exists( 'use_block_editor_for_post_type' ) |
| 28 | // && use_block_editor_for_post_type( $post_type_slug ) |
| 29 | // ) { |
| 30 | // continue; // go to the beginning of next iteration |
| 31 | // } |
| 32 | |
| 33 | add_meta_box( |
| 34 | 'asenha-external-permalink', // ID of meta box |
| 35 | 'External Permalink', // Title of meta box |
| 36 | [ $this, 'output_external_permalink_meta_box' ], // Callback function |
| 37 | $post_type_slug, // The screen on which the meta box should be output to |
| 38 | 'normal', // context |
| 39 | 'high' // priority |
| 40 | // array(), // $args to pass to callback function. Ref: https://developer.wordpress.org/reference/functions/add_meta_box/#comment-342 |
| 41 | ); |
| 42 | |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Render External Permalink meta box |
| 50 | * |
| 51 | * @since 3.9.0 |
| 52 | */ |
| 53 | public function output_external_permalink_meta_box( $post ) { |
| 54 | ?> |
| 55 | <div class="external-permalink-input"> |
| 56 | <input name="<?php echo esc_attr( 'external_permalink' ); ?>" class="large-text" id="<?php echo esc_attr( 'external_permalink' ); ?>" type="text" value="<?php echo esc_url( get_post_meta( $post->ID, '_links_to', true ) ); ?>" placeholder="https://" /> |
| 57 | <div class="external-permalink-input-description">Keep empty to use the default WordPress permalink. External permalink will open in a new browser tab.</div> |
| 58 | <?php wp_nonce_field( 'external_permalink_' . $post->ID, 'external_permalink_nonce', false, true ); ?> |
| 59 | </div> |
| 60 | <?php |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Save external permalink input |
| 65 | * |
| 66 | * @since 3.9.0 |
| 67 | */ |
| 68 | public function save_external_permalink( $post_id ) { |
| 69 | |
| 70 | // Only proceed if nonce is verified |
| 71 | if ( isset( $_POST['external_permalink_nonce'] ) && wp_verify_nonce( $_POST['external_permalink_nonce'], 'external_permalink_' . $post_id ) ) { |
| 72 | |
| 73 | // Get the value of external permalink from input field |
| 74 | $external_permalink = isset( $_POST['external_permalink'] ) ? esc_url_raw( trim( $_POST['external_permalink'] ) ) : ''; |
| 75 | |
| 76 | // Update or delete external permalink post meta |
| 77 | if ( ! empty( $external_permalink ) ) { |
| 78 | update_post_meta( $post_id, '_links_to', $external_permalink ); |
| 79 | } else { |
| 80 | delete_post_meta( $post_id, '_links_to' ); |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Change WordPress default permalink into external permalink for pages |
| 89 | * |
| 90 | * @since 3.9.0 |
| 91 | */ |
| 92 | public function use_external_permalink_for_pages( $permalink, $post_id ) { |
| 93 | |
| 94 | $request_uri = sanitize_text_field( $_SERVER['REQUEST_URI'] ); // e.g. /wp-admin/index.php?page=page-slug |
| 95 | |
| 96 | if ( false === strpos( $request_uri, 'mfn-live-builder' ) ) { |
| 97 | // When not in BeTheme template builder, that has the 'action=mfn-live-builder' parameter in the URL |
| 98 | |
| 99 | $external_permalink = get_post_meta( $post_id, '_links_to', true ); |
| 100 | |
| 101 | if ( ! empty( $external_permalink ) ) { |
| 102 | $permalink = $external_permalink; |
| 103 | } |
| 104 | |
| 105 | } |
| 106 | |
| 107 | return $permalink; |
| 108 | |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Change WordPress default permalink into external permalink for posts and custom post types |
| 113 | * |
| 114 | * @since 3.9.0 |
| 115 | */ |
| 116 | public function use_external_permalink_for_posts( $permalink, $post ) { |
| 117 | |
| 118 | $request_uri = sanitize_text_field( $_SERVER['REQUEST_URI'] ); // e.g. /wp-admin/index.php?page=page-slug |
| 119 | |
| 120 | if ( false === strpos( $request_uri, 'mfn-live-builder' ) ) { |
| 121 | // When not in BeTheme template builder, that has the 'action=mfn-live-builder' parameter in the URL |
| 122 | |
| 123 | $external_permalink = get_post_meta( $post->ID, '_links_to', true ); |
| 124 | |
| 125 | if ( ! empty( $external_permalink ) ) { |
| 126 | $permalink = $external_permalink; |
| 127 | |
| 128 | if ( ! is_admin() ) { |
| 129 | $permalink = $permalink . '#new_tab'; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | } |
| 134 | |
| 135 | return $permalink; |
| 136 | |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Redirect page/post to external permalink if it's loaded directly from the WP default permalink |
| 141 | * |
| 142 | * @since 3.9.0 |
| 143 | */ |
| 144 | public function redirect_to_external_permalink() { |
| 145 | |
| 146 | // If not on/loading the single page/post URL, do nothing |
| 147 | if ( ! is_singular() ) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | global $post; |
| 152 | |
| 153 | $external_permalink = get_post_meta( $post->ID, '_links_to', true ); |
| 154 | |
| 155 | if ( ! empty( $external_permalink ) ) { |
| 156 | wp_redirect( $external_permalink, 302 ); // temporary redirect |
| 157 | exit; |
| 158 | } |
| 159 | |
| 160 | } |
| 161 | |
| 162 | } |