| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle permalink functions |
| 4 |
* |
| 5 |
* @package UpStream |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
add_action( 'admin_init', 'upstream_register_permalink_settings' ); |
| 14 |
add_action( 'admin_init', 'upstream_validate_permalink_settings' ); |
| 15 |
|
| 16 |
/** |
| 17 |
* Returns the permalink base for projects and client. |
| 18 |
* |
| 19 |
* @param string $base Base name. |
| 20 |
* |
| 21 |
* @return string|false |
| 22 |
*/ |
| 23 |
function upstream_get_permalink_base( $base ) { |
| 24 |
if ( ! in_array( $base, array( 'projects', 'client' ) ) ) { |
| 25 |
return false; |
| 26 |
} |
| 27 |
|
| 28 |
$default = trim( sanitize_title( apply_filters( 'upstream_' . $base . '_base', $base ) ) ); |
| 29 |
|
| 30 |
$base = trim( sanitize_title( get_option( 'upstream_' . $base . '_base', $default ) ) ); |
| 31 |
|
| 32 |
if ( empty( $base ) ) { |
| 33 |
$base = $default; |
| 34 |
} |
| 35 |
|
| 36 |
return $base; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Returns the project base segment for permalinks. |
| 41 |
* |
| 42 |
* @return mixed |
| 43 |
*/ |
| 44 |
function upstream_get_project_base() { |
| 45 |
return upstream_get_permalink_base( 'projects' ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Upstream_is_project_base_uri |
| 50 |
* |
| 51 |
* @param string $uri URI. |
| 52 |
*/ |
| 53 |
function upstream_is_project_base_uri( $uri ) { |
| 54 |
$pb = upstream_get_project_base(); |
| 55 |
|
| 56 |
if ( '/' . $pb === $uri ) { |
| 57 |
return true; |
| 58 |
} elseif ( '/' . $pb . '/' === $uri ) { |
| 59 |
return true; |
| 60 |
} elseif ( preg_match( '/^\/' . $pb . '\?/i', $uri ) ) { |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns the client base segment for permalinks. |
| 69 |
* |
| 70 |
* @return mixed |
| 71 |
*/ |
| 72 |
function upstream_get_client_base() { |
| 73 |
return upstream_get_permalink_base( 'client' ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Register settings for the permalink. |
| 78 |
*/ |
| 79 |
function upstream_register_permalink_settings() { |
| 80 |
/* |
| 81 |
* Section |
| 82 |
*/ |
| 83 |
add_settings_section( |
| 84 |
'upstream', |
| 85 |
__( 'UpStream Settings', 'upstream' ), |
| 86 |
'upstream_permalink_settings_section', |
| 87 |
'permalink' |
| 88 |
); |
| 89 |
|
| 90 |
/* |
| 91 |
* Fields |
| 92 |
*/ |
| 93 |
add_settings_field( |
| 94 |
'upstream_projects_permalink', |
| 95 |
__( 'Projects base', 'upstream' ), |
| 96 |
'upstream_print_project_permalink_field', |
| 97 |
'permalink', |
| 98 |
'upstream' |
| 99 |
); |
| 100 |
|
| 101 |
add_settings_field( |
| 102 |
'upstream_client_permalink', |
| 103 |
__( 'Client base', 'upstream' ), |
| 104 |
'upstream_print_client_permalink_field', |
| 105 |
'permalink', |
| 106 |
'upstream' |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Prints the field for the projects' permalink base. |
| 112 |
*/ |
| 113 |
function upstream_print_project_permalink_field() { |
| 114 |
$value = esc_attr( get_option( 'upstream_projects_base', '' ) ); |
| 115 |
$default = esc_attr( apply_filters( 'upstream_projects_base', 'projects' ) ); |
| 116 |
|
| 117 |
echo '<input name="upstream_projects_base" id="upstream_projects_base" type="text" class="regular-text code" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $default ) . '">'; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Prints the field for the client's permalink base. |
| 122 |
*/ |
| 123 |
function upstream_print_client_permalink_field() { |
| 124 |
$value = esc_attr( get_option( 'upstream_client_base', '' ) ); |
| 125 |
$default = esc_attr( apply_filters( 'upstream_client_base', 'client' ) ); |
| 126 |
|
| 127 |
echo '<input name="upstream_client_base" id="upstream_client_base" type="text" class="regular-text code" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $default ) . '">'; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Validates and save permalink settings. |
| 132 |
*/ |
| 133 |
function upstream_validate_permalink_settings() { |
| 134 |
$post_data = isset( $_POST ) ? wp_unslash( $_POST ) : array(); |
| 135 |
|
| 136 |
if ( ! array_key_exists( 'permalink_structure', $post_data ) ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
if ( ! array_key_exists( 'upstream_nonce', $post_data ) ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
if ( ! wp_verify_nonce( sanitize_text_field( $post_data['upstream_nonce'] ), 'upstream_permalink_settings' ) ) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
if ( array_key_exists( 'upstream_projects_base', $post_data ) ) { |
| 149 |
$option = sanitize_title( $post_data['upstream_projects_base'] ); |
| 150 |
update_option( 'upstream_projects_base', $option ); |
| 151 |
} |
| 152 |
|
| 153 |
if ( array_key_exists( 'upstream_client_base', $post_data ) ) { |
| 154 |
$option = sanitize_title( $post_data['upstream_client_base'] ); |
| 155 |
update_option( 'upstream_client_base', $option ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Prints the output for the permalink section. |
| 161 |
*/ |
| 162 |
function upstream_permalink_settings_section() { |
| 163 |
wp_nonce_field( 'upstream_permalink_settings', 'upstream_nonce' ); |
| 164 |
} |
| 165 |
|