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