| 1 |
<?php |
| 2 |
/** |
| 3 |
* Adds settings to the permalinks admin settings page. |
| 4 |
* |
| 5 |
* @class FT_Admin_Permalink_Settings |
| 6 |
* @version 1.0.0 |
| 7 |
* @package FlashToolkit/Admin |
| 8 |
* @category Admin |
| 9 |
* @author ThemeGrill |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
if ( ! class_exists( 'FT_Admin_Permalink_Settings', false ) ) : |
| 17 |
|
| 18 |
/** |
| 19 |
* FT_Admin_Permalink_Settings Class |
| 20 |
*/ |
| 21 |
class FT_Admin_Permalink_Settings { |
| 22 |
|
| 23 |
/** |
| 24 |
* Permalink settings. |
| 25 |
* |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
private $permalinks = array(); |
| 29 |
|
| 30 |
/** |
| 31 |
* Hook in tabs. |
| 32 |
*/ |
| 33 |
public function __construct() { |
| 34 |
$this->settings_init(); |
| 35 |
$this->settings_save(); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Init our settings. |
| 40 |
*/ |
| 41 |
public function settings_init() { |
| 42 |
// Add a section to the permalinks page |
| 43 |
add_settings_section( 'flash-toolkit-permalink', __( 'Portfolio Permalinks', 'flash-toolkit' ), array( $this, 'settings' ), 'permalink' ); |
| 44 |
|
| 45 |
// Add our settings |
| 46 |
add_settings_field( |
| 47 |
'flash_toolkit_portfolio_category_slug', // id |
| 48 |
__( 'Portfolio category base', 'flash-toolkit' ), // setting title |
| 49 |
array( $this, 'portfolio_category_slug_input' ), // display callback |
| 50 |
'permalink', // settings page |
| 51 |
'optional' // settings section |
| 52 |
); |
| 53 |
add_settings_field( |
| 54 |
'flash_toolkit_portfolio_tag_slug', // id |
| 55 |
__( 'Portfolio tag base', 'flash-toolkit' ), // setting title |
| 56 |
array( $this, 'portfolio_tag_slug_input' ), // display callback |
| 57 |
'permalink', // settings page |
| 58 |
'optional' // settings section |
| 59 |
); |
| 60 |
|
| 61 |
$this->permalinks = flash_get_permalink_structure(); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Show a slug input box. |
| 66 |
*/ |
| 67 |
public function portfolio_category_slug_input() { |
| 68 |
?> |
| 69 |
<input name="flash_toolkit_portfolio_category_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $this->permalinks['category_base'] ); ?>" placeholder="<?php echo esc_attr_x( 'portfolio-category', 'slug', 'flash-toolkit') ?>" /> |
| 70 |
<?php |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Show a slug input box. |
| 75 |
*/ |
| 76 |
public function portfolio_tag_slug_input() { |
| 77 |
?> |
| 78 |
<input name="flash_toolkit_portfolio_tag_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $this->permalinks['tag_base'] ); ?>" placeholder="<?php echo esc_attr_x( 'portfolio-tag', 'slug', 'flash-toolkit' ) ?>" /> |
| 79 |
<?php |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Show the settings. |
| 84 |
*/ |
| 85 |
public function settings() { |
| 86 |
echo wpautop( __( 'These settings control the permalinks specifically used for portfolio.', 'flash-toolkit' ) ); |
| 87 |
|
| 88 |
// Get base slug |
| 89 |
$base_slug = _x( 'project', 'default-slug', 'flash-toolkit' ); |
| 90 |
$portfolio_base = _x( 'portfolio', 'default-slug', 'flash-toolkit' ); |
| 91 |
|
| 92 |
$structures = array( |
| 93 |
0 => '', |
| 94 |
1 => '/' . trailingslashit( $base_slug ), |
| 95 |
2 => '/' . trailingslashit( $base_slug ) . trailingslashit( '%portfolio_cat%' ) |
| 96 |
); |
| 97 |
?> |
| 98 |
<table class="form-table flash-permalink-structure"> |
| 99 |
<tbody> |
| 100 |
<tr> |
| 101 |
<th><label><input name="portfolio_permalink" type="radio" value="<?php echo esc_attr( $structures[0] ); ?>" class="flash-tog" <?php checked( $structures[0], $this->permalinks['portfolio_base'] ); ?> /> <?php _e( 'Default', 'flash-toolkit' ); ?></label></th> |
| 102 |
<td><code class="default-example"><?php echo esc_html( home_url() ); ?>/?portfolio=sample-portfolio</code> <code class="non-default-example"><?php echo esc_html( home_url() ); ?>/<?php echo esc_html( $portfolio_base ); ?>/sample-portfolio/</code></td> |
| 103 |
</tr> |
| 104 |
<tr> |
| 105 |
<th><label><input name="portfolio_permalink" type="radio" value="<?php echo esc_attr( $structures[1] ); ?>" class="flash-tog" <?php checked( $structures[1], $this->permalinks['portfolio_base'] ); ?> /> <?php _e( 'Project base', 'flash-toolkit' ); ?></label></th> |
| 106 |
<td><code><?php echo esc_html( home_url() ); ?>/<?php echo esc_html( $base_slug ); ?>/sample-portfolio/</code></td> |
| 107 |
</tr> |
| 108 |
<tr> |
| 109 |
<th><label><input name="portfolio_permalink" type="radio" value="<?php echo esc_attr( $structures[2] ); ?>" class="flash-tog" <?php checked( $structures[2], $this->permalinks['portfolio_base'] ); ?> /> <?php _e( 'Project based category', 'flash-toolkit' ); ?></label></th> |
| 110 |
<td><code><?php echo esc_html( home_url() ); ?>/<?php echo esc_html( $base_slug ); ?>/portfolio-category/sample-portfolio/</code></td> |
| 111 |
</tr> |
| 112 |
<tr> |
| 113 |
<th><label><input name="portfolio_permalink" id="flash_toolkit_custom_selection" type="radio" value="custom" class="tog" <?php checked( in_array( $this->permalinks['portfolio_base'], $structures ), false ); ?> /> |
| 114 |
<?php _e( 'Custom Base', 'flash-toolkit' ); ?></label></th> |
| 115 |
<td> |
| 116 |
<input name="portfolio_permalink_structure" id="flash_toolkit_permalink_structure" type="text" value="<?php echo esc_attr( $this->permalinks['portfolio_base'] ? trailingslashit( $this->permalinks['portfolio_base'] ) : '' ); ?>" class="regular-text code"> <span class="description"><?php _e( 'Enter a custom base to use. A base <strong>must</strong> be set or WordPress will use default instead.', 'flash-toolkit' ); ?></span> |
| 117 |
</td> |
| 118 |
</tr> |
| 119 |
</tbody> |
| 120 |
</table> |
| 121 |
<script type="text/javascript"> |
| 122 |
jQuery( function() { |
| 123 |
jQuery( 'input.flash-tog' ).change( function() { |
| 124 |
jQuery( '#flash_toolkit_permalink_structure' ).val( jQuery( this ).val() ); |
| 125 |
}); |
| 126 |
jQuery( '.permalink-structure input' ).change(function() { |
| 127 |
jQuery( '.flash-permalink-structure' ).find( 'code.non-default-example, code.default-example' ).hide(); |
| 128 |
if ( jQuery( this ).val() ) { |
| 129 |
jQuery( '.flash-permalink-structure code.non-default-example' ).show(); |
| 130 |
jQuery( '.flash-permalink-structure input').removeAttr( 'disabled' ); |
| 131 |
} else { |
| 132 |
jQuery( '.flash-permalink-structure code.default-example' ).show(); |
| 133 |
jQuery( '.flash-permalink-structure input:eq(0)' ).click(); |
| 134 |
jQuery( '.flash-permalink-structure input' ).attr( 'disabled', 'disabled' ); |
| 135 |
} |
| 136 |
}); |
| 137 |
jQuery( '.permalink-structure input:checked' ).change(); |
| 138 |
jQuery( '#flash_toolkit_permalink_structure' ).focus( function() { |
| 139 |
jQuery( '#flash_toolkit_custom_selection' ).click(); |
| 140 |
}); |
| 141 |
} ); |
| 142 |
</script> |
| 143 |
<?php |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Save the settings. |
| 148 |
*/ |
| 149 |
public function settings_save() { |
| 150 |
if ( ! is_admin() ) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
// We need to save the options ourselves; settings api does not trigger save for the permalinks page. |
| 155 |
if ( isset( $_POST['permalink_structure'] ) ) { |
| 156 |
flash_switch_to_site_locale(); |
| 157 |
|
| 158 |
$permalinks = (array) get_option( 'flash_toolkit_permalinks', array() ); |
| 159 |
$permalinks['category_base'] = flash_sanitize_permalink( trim( $_POST['flash_toolkit_portfolio_category_slug'] ) ); |
| 160 |
$permalinks['tag_base'] = flash_sanitize_permalink( trim( $_POST['flash_toolkit_portfolio_tag_slug'] ) ); |
| 161 |
|
| 162 |
// Generate portfolio base. |
| 163 |
$portfolio_base = isset( $_POST['portfolio_permalink'] ) ? flash_clean( $_POST['portfolio_permalink'] ) : ''; |
| 164 |
|
| 165 |
if ( 'custom' === $portfolio_base ) { |
| 166 |
if ( isset( $_POST['portfolio_permalink_structure'] ) ) { |
| 167 |
$portfolio_base = preg_replace( '#/+#', '/', '/' . str_replace( '#', '', trim( $_POST['portfolio_permalink_structure'] ) ) ); |
| 168 |
} else { |
| 169 |
$portfolio_base = '/'; |
| 170 |
} |
| 171 |
|
| 172 |
// This is an invalid base structure and breaks pages. |
| 173 |
if ( '/%portfolio_cat%/' === trailingslashit( $portfolio_base ) ) { |
| 174 |
$portfolio_base = '/' . _x( 'portfolio', 'slug', 'flash-toolkit' ) . $portfolio_base; |
| 175 |
} |
| 176 |
} elseif ( empty( $portfolio_base ) ) { |
| 177 |
$portfolio_base = false; |
| 178 |
} |
| 179 |
|
| 180 |
$permalinks['portfolio_base'] = flash_sanitize_permalink( $portfolio_base ); |
| 181 |
|
| 182 |
// Ensure portfolio archive slugs are set. |
| 183 |
$permalinks['portfolio_has_archive'] = true; |
| 184 |
|
| 185 |
if ( $permalinks['portfolio_base'] ) { |
| 186 |
$portfolio_slug = explode( '/', trim( $permalinks['portfolio_base'], '/' ) ); |
| 187 |
|
| 188 |
if ( $portfolio_slug ) { |
| 189 |
$permalinks['portfolio_has_archive'] = $portfolio_slug[0]; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
update_option( 'flash_toolkit_permalinks', $permalinks ); |
| 194 |
|
| 195 |
flash_restore_locale(); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
endif; |
| 201 |
|
| 202 |
return new FT_Admin_Permalink_Settings(); |
| 203 |
|