permalink-manager-actions.php
1 year ago
permalink-manager-admin-functions.php
1 year ago
permalink-manager-core-functions.php
1 year ago
permalink-manager-debug.php
1 year ago
permalink-manager-gutenberg.php
1 year ago
permalink-manager-helper-functions.php
1 year ago
permalink-manager-permastructures-functions.php
1 year ago
permalink-manager-uri-functions-post.php
1 year ago
permalink-manager-uri-functions.php
1 year ago
permalink-manager-permastructures-functions.php
139 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Helper functions used for Permastructures (permalink formats) |
| 5 | */ |
| 6 | class Permalink_Manager_Permastructure_Functions { |
| 7 | |
| 8 | public function __construct() { |
| 9 | add_action( 'plugins_loaded', array( $this, 'init' ), 5 ); |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Add hooks used by plugin to filter the custom permalinks |
| 14 | */ |
| 15 | function init() { |
| 16 | // Replace empty placeholder tags & remove BOM |
| 17 | add_filter( 'permalink_manager_filter_default_post_uri', array( $this, 'replace_empty_placeholder_tags' ), 10, 5 ); |
| 18 | add_filter( 'permalink_manager_filter_default_term_uri', array( $this, 'replace_empty_placeholder_tags' ), 10, 5 ); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Get the default permalink format for specific post type |
| 23 | * |
| 24 | * @param string $post_type |
| 25 | * @param bool $remove_post_tag |
| 26 | * |
| 27 | * @return string |
| 28 | */ |
| 29 | static function get_default_permastruct( $post_type = 'page', $remove_post_tag = false ) { |
| 30 | global $wp_rewrite; |
| 31 | |
| 32 | // Get default permastruct |
| 33 | if ( $post_type == 'page' ) { |
| 34 | $permastruct = $wp_rewrite->get_page_permastruct(); |
| 35 | } else if ( $post_type == 'post' ) { |
| 36 | $permastruct = get_option( 'permalink_structure' ); |
| 37 | } else { |
| 38 | $permastruct = $wp_rewrite->get_extra_permastruct( $post_type ); |
| 39 | } |
| 40 | |
| 41 | return ( $remove_post_tag ) ? trim( str_replace( array( "%postname%", "%pagename%", "%{$post_type}%" ), "", $permastruct ), "/" ) : $permastruct; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the post name permastructure tag for specific post type |
| 46 | * |
| 47 | * @param string $post_type |
| 48 | * |
| 49 | * @return string |
| 50 | */ |
| 51 | static function get_post_tag( $post_type ) { |
| 52 | // Get the post type (with fix for posts & pages) |
| 53 | if ( $post_type == 'page' ) { |
| 54 | $post_type_tag = '%pagename%'; |
| 55 | } else if ( $post_type == 'post' ) { |
| 56 | $post_type_tag = '%postname%'; |
| 57 | } else { |
| 58 | $post_type_tag = "%{$post_type}%"; |
| 59 | } |
| 60 | |
| 61 | return $post_type_tag; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Check if any of slug tags is present inside Permastructure settings |
| 66 | * |
| 67 | * @param $default_uri |
| 68 | * @param $slug_tags |
| 69 | * @param $content_element |
| 70 | * |
| 71 | * @return bool|null |
| 72 | */ |
| 73 | public static function is_slug_tag_present( $default_uri, $slug_tags, $content_element ) { |
| 74 | global $permalink_manager_options; |
| 75 | |
| 76 | // Check if any post tag is present in custom permastructure |
| 77 | if ( ! empty( $content_element->post_type ) ) { |
| 78 | $content_type = $content_element->post_type; |
| 79 | $content_type_key = 'post_types'; |
| 80 | } else if ( ! empty( $content_element->taxonomy ) ) { |
| 81 | $content_type = $content_element->taxonomy; |
| 82 | $content_type_key = 'taxonomies'; |
| 83 | } else { |
| 84 | return null; |
| 85 | } |
| 86 | |
| 87 | $permastructure_settings = ( ! empty( $permalink_manager_options['permastructure-settings'] ) ) ? $permalink_manager_options['permastructure-settings'] : array(); |
| 88 | $do_not_append_settings = ( ! empty( $permastructure_settings['do_not_append_slug'] ) ) ? $permastructure_settings['do_not_append_slug'] : array(); |
| 89 | $do_not_append_slug = ( ! empty( $do_not_append_settings[ $content_type_key ] ) && ! empty( $do_not_append_settings[ $content_type_key ][ $content_type ] ) ) ? true : false; |
| 90 | $do_not_append_slug = apply_filters( "permalink_manager_do_not_append_slug", $do_not_append_slug, $content_type, $content_element ); |
| 91 | |
| 92 | if ( ! $do_not_append_slug ) { |
| 93 | foreach ( $slug_tags as $tag ) { |
| 94 | if ( strpos( $default_uri, $tag ) !== false ) { |
| 95 | $do_not_append_slug = true; |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // 3F. Replace the post tags with slugs or append the slug if no post tag is defined |
| 102 | if ( ! empty( $do_not_append_slug ) ) { |
| 103 | return true; |
| 104 | } else { |
| 105 | return false; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Replace empty placeholder tags & remove BOM |
| 111 | * |
| 112 | * @param string $default_uri |
| 113 | * @param string $native_slug |
| 114 | * @param string $element |
| 115 | * @param string $slug |
| 116 | * @param bool $native_uri |
| 117 | * |
| 118 | * @return string |
| 119 | */ |
| 120 | public static function replace_empty_placeholder_tags( $default_uri, $native_slug = "", $element = "", $slug = "", $native_uri = false ) { |
| 121 | // Remove the BOM |
| 122 | $default_uri = str_replace( array( "\xEF\xBB\xBF", "%ef%bb%bf" ), '', $default_uri ); |
| 123 | |
| 124 | // Encode the URI before placeholders are removed |
| 125 | $chunks = explode( '/', $default_uri ); |
| 126 | foreach ( $chunks as &$chunk ) { |
| 127 | if ( ! preg_match( "/^(%.+?%)$/", $chunk ) && preg_match( '/%[A-F0-9]{2}%[A-F0-9]{2}/i', $chunk ) ) { |
| 128 | $chunk = rawurldecode( $chunk ); |
| 129 | } |
| 130 | } |
| 131 | $default_uri = implode( "/", $chunks ); |
| 132 | |
| 133 | $empty_tag_replacement = apply_filters( 'permalink_manager_empty_tag_replacement', '', $element ); |
| 134 | $default_uri = preg_replace( "/%(.+?)%/", $empty_tag_replacement, $default_uri ); |
| 135 | $default_uri = str_replace( "//", "/", $default_uri ); |
| 136 | |
| 137 | return trim( $default_uri, "/" ); |
| 138 | } |
| 139 | } |