| 1 |
<?php |
| 2 |
|
| 3 |
add_action( 'admin_menu', 'mtphr_post_duplicator_settings_page' ); |
| 4 |
/** |
| 5 |
* Add a menu page to display options |
| 6 |
* |
| 7 |
* @since 2.0 |
| 8 |
*/ |
| 9 |
function mtphr_post_duplicator_settings_page() { |
| 10 |
|
| 11 |
add_management_page( |
| 12 |
esc_html__('Post Duplicator', 'post-duplicator'), // The value used to populate the browser's title bar when the menu page is active |
| 13 |
esc_html__('Post Duplicator', 'post-duplicator'), // The label of this submenu item displayed in the menu |
| 14 |
'administrator', // What roles are able to access this submenu item |
| 15 |
'mtphr_post_duplicator_settings_menu', // The ID used to represent this submenu item |
| 16 |
'mtphr_post_duplicator_settings_display' // The callback function used to render the options for this submenu item |
| 17 |
); |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
add_action( 'admin_init', 'mtphr_post_duplicator_initialize_settings' ); |
| 24 |
/** |
| 25 |
* Initializes the options page. |
| 26 |
* |
| 27 |
* @since 2.17 |
| 28 |
*/ |
| 29 |
function mtphr_post_duplicator_initialize_settings() { |
| 30 |
|
| 31 |
$options = get_option( 'mtphr_post_duplicator_settings', array() ); |
| 32 |
|
| 33 |
$settings['post_duplication'] = array( |
| 34 |
'title' => esc_html__( 'Post Duplication', 'post-duplicator' ), |
| 35 |
'type' => 'radio', |
| 36 |
'options' => array( |
| 37 |
'all_users' => esc_html__('Allow Duplication of All Users', 'post-duplicator'), |
| 38 |
'current_user' => esc_html__('Limit to Current User', 'post-duplicator') |
| 39 |
), |
| 40 |
'display' => 'inline', |
| 41 |
'default' => isset( $options['post_duplication'] ) ? sanitize_text_field( $options['post_duplication'] ) : 'all_users' |
| 42 |
); |
| 43 |
|
| 44 |
$settings['post_author'] = array( |
| 45 |
'title' => esc_html__( 'Post Author', 'post-duplicator' ), |
| 46 |
'type' => 'radio', |
| 47 |
'options' => array( |
| 48 |
'current_user' => esc_html__('Current User', 'post-duplicator'), |
| 49 |
'original_user' => esc_html__('Original Post Author', 'post-duplicator'), |
| 50 |
), |
| 51 |
'display' => 'inline', |
| 52 |
'default' => isset( $options['post_author'] ) ? sanitize_text_field( $options['post_author'] ) : 'current_user' |
| 53 |
); |
| 54 |
|
| 55 |
$settings['status'] = array( |
| 56 |
'title' => esc_html__( 'Post Status', 'post-duplicator' ), |
| 57 |
'type' => 'select', |
| 58 |
'options' => array( |
| 59 |
'same' => esc_html__('Same as original', 'post-duplicator'), |
| 60 |
'draft' => esc_html__('Draft', 'post-duplicator'), |
| 61 |
'publish' => esc_html__('Published', 'post-duplicator'), |
| 62 |
'pending' => esc_html__('Pending', 'post-duplicator') |
| 63 |
), |
| 64 |
'default' => isset( $options['status'] ) ? sanitize_text_field( $options['status'] ) : 'draft' |
| 65 |
); |
| 66 |
|
| 67 |
$settings['type'] = array( |
| 68 |
'title' => esc_html__( 'Post Type', 'post-duplicator' ), |
| 69 |
'type' => 'select', |
| 70 |
'options' => mtphr_post_duplicator_post_types(), |
| 71 |
'default' => isset( $options['type'] ) ? sanitize_text_field( $options['type'] ) : 'same' |
| 72 |
); |
| 73 |
|
| 74 |
$settings['timestamp'] = array( |
| 75 |
'title' => esc_html__( 'Post Date', 'post-duplicator' ), |
| 76 |
'type' => 'radio', |
| 77 |
'options' => array( |
| 78 |
'duplicate' => esc_html__('Duplicate Timestamp', 'post-duplicator'), |
| 79 |
'current' => esc_html__('Current Time', 'post-duplicator') |
| 80 |
), |
| 81 |
'display' => 'inline', |
| 82 |
'default' => isset( $options['timestamp'] ) ? sanitize_text_field( $options['timestamp'] ) : 'current' |
| 83 |
); |
| 84 |
|
| 85 |
$settings['title'] = array( |
| 86 |
'title' => esc_html__( 'Duplicate Title', 'post-duplicator' ), |
| 87 |
'description' => esc_html__('String that should be appended to the duplicate post\'s title', 'post-duplicator'), |
| 88 |
'type' => 'text', |
| 89 |
'display' => 'inline', |
| 90 |
'default' => isset( $options['title'] ) ? sanitize_text_field( $options['title'] ) : esc_html__('Copy', 'post-duplicator') |
| 91 |
); |
| 92 |
|
| 93 |
$settings['slug'] = array( |
| 94 |
'title' => esc_html__( 'Duplicate Slug', 'post-duplicator' ), |
| 95 |
'description' => esc_html__('String that should be appended to the duplicate post\'s slug', 'post-duplicator'), |
| 96 |
'type' => 'text', |
| 97 |
'display' => 'inline', |
| 98 |
'default' => isset( $options['slug'] ) ? sanitize_text_field( $options['slug'] ) : 'copy' |
| 99 |
); |
| 100 |
|
| 101 |
$settings['time_offset'] = array( |
| 102 |
'title' => esc_html__( 'Offset Date', 'post-duplicator' ), |
| 103 |
'type' => 'checkbox', |
| 104 |
'default' => isset( $options['time_offset'] ) ? sanitize_text_field( $options['time_offset'] ) : 0, |
| 105 |
'append' => array( |
| 106 |
'time_offset_days' => array( |
| 107 |
'type' => 'text', |
| 108 |
'size' => 2, |
| 109 |
'after' => esc_html__(' days', 'post-duplicator'), |
| 110 |
'text_align' => 'right', |
| 111 |
'default' => isset( $options['time_offset_days'] ) ? sanitize_text_field( $options['time_offset_days'] ) : 0 |
| 112 |
), |
| 113 |
'time_offset_hours' => array( |
| 114 |
'type' => 'text', |
| 115 |
'size' => 2, |
| 116 |
'after' => esc_html__(' hours', 'post-duplicator'), |
| 117 |
'text_align' => 'right', |
| 118 |
'default' => isset( $options['time_offset_hours'] ) ? sanitize_text_field( $options['time_offset_hours'] ) : 0 |
| 119 |
), |
| 120 |
'time_offset_minutes' => array( |
| 121 |
'type' => 'text', |
| 122 |
'size' => 2, |
| 123 |
'after' => esc_html__(' minutes', 'post-duplicator'), |
| 124 |
'text_align' => 'right', |
| 125 |
'default' => isset( $options['time_offset_minutes'] ) ? sanitize_text_field( $options['time_offset_minutes'] ) : 0 |
| 126 |
), |
| 127 |
'time_offset_seconds' => array( |
| 128 |
'type' => 'text', |
| 129 |
'size' => 2, |
| 130 |
'after' => esc_html__(' seconds', 'post-duplicator'), |
| 131 |
'text_align' => 'right', |
| 132 |
'default' => isset( $options['time_offset_seconds'] ) ? sanitize_text_field( $options['time_offset_seconds'] ) : 0 |
| 133 |
), |
| 134 |
'time_offset_direction' => array( |
| 135 |
'type' => 'select', |
| 136 |
'options' => array( |
| 137 |
'newer' => esc_html__('newer', 'post-duplicator'), |
| 138 |
'older' => esc_html__('older', 'post-duplicator') |
| 139 |
), |
| 140 |
'default' => isset( $options['time_offset_direction'] ) ? sanitize_text_field( $options['time_offset_direction'] ) : 'newer' |
| 141 |
) |
| 142 |
) |
| 143 |
); |
| 144 |
|
| 145 |
if( false == get_option('mtphr_post_duplicator_settings') ) { |
| 146 |
add_option( 'mtphr_post_duplicator_settings' ); |
| 147 |
} |
| 148 |
|
| 149 |
/* Register the style options */ |
| 150 |
add_settings_section( |
| 151 |
'mtphr_post_duplicator_settings_section', // ID used to identify this section and with which to register options |
| 152 |
'', // Title to be displayed on the administration page |
| 153 |
'mtphr_post_duplicator_settings_callback', // Callback used to render the description of the section |
| 154 |
'mtphr_post_duplicator_settings' // Page on which to add this section of options |
| 155 |
); |
| 156 |
|
| 157 |
$settings = apply_filters( 'mtphr_post_duplicator_settings', $settings ); |
| 158 |
|
| 159 |
if( is_array($settings) ) { |
| 160 |
foreach( $settings as $id => $setting ) { |
| 161 |
$setting['option_id'] = $id; |
| 162 |
$setting['id'] = 'mtphr_post_duplicator_settings['.$id.']'; |
| 163 |
add_settings_field( $setting['id'], $setting['title'], 'mtphr_post_duplicator_field_display', 'mtphr_post_duplicator_settings', 'mtphr_post_duplicator_settings_section', $setting); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
// Register the fields with WordPress |
| 168 |
register_setting( 'mtphr_post_duplicator_settings', 'mtphr_post_duplicator_settings', 'mtphr_post_duplicator_settings_sanitize' ); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
/** |
| 173 |
* Sanitize the settings |
| 174 |
* |
| 175 |
* @since 2.27 |
| 176 |
*/ |
| 177 |
function mtphr_post_duplicator_settings_sanitize( $fields ) { |
| 178 |
$sanitized_fields = array( |
| 179 |
'post_duplication' => isset( $fields['post_duplication'] ) ? sanitize_text_field( $fields['post_duplication'] ) : 'all_users', |
| 180 |
'post_author' => isset( $fields['post_author'] ) ? sanitize_text_field( $fields['post_author'] ) : 'current_user', |
| 181 |
'status' => isset( $fields['status'] ) ? sanitize_text_field( $fields['status'] ) : 'draft', |
| 182 |
'type' => isset( $fields['type'] ) ? sanitize_text_field( $fields['type'] ) : 'same', |
| 183 |
'timestamp' => isset( $fields['timestamp'] ) ? sanitize_text_field( $fields['timestamp'] ) : 'current', |
| 184 |
'title' => isset( $fields['title'] ) ? sanitize_text_field( $fields['title'] ) : '', |
| 185 |
'slug' => isset( $fields['slug'] ) ? sanitize_title_with_dashes( $fields['slug'] ) : '', |
| 186 |
'time_offset' => isset( $fields['time_offset'] ) ? sanitize_text_field( $fields['time_offset'] ) : false, |
| 187 |
'time_offset_days' => isset( $fields['time_offset_days'] ) ? intval( $fields['time_offset_days'] ) : 0, |
| 188 |
'time_offset_hours' => isset( $fields['time_offset_hours'] ) ? intval( $fields['time_offset_hours'] ) : 0, |
| 189 |
'time_offset_minutes' => isset( $fields['time_offset_minutes'] ) ? intval( $fields['time_offset_minutes'] ) : 0, |
| 190 |
'time_offset_seconds' => isset( $fields['time_offset_seconds'] ) ? intval( $fields['time_offset_seconds'] ) : 0, |
| 191 |
'time_offset_direction' => isset( $fields['time_offset_direction'] ) ? sanitize_text_field( $fields['time_offset_direction'] ) : 'newer', |
| 192 |
); |
| 193 |
return $sanitized_fields; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
/** |
| 200 |
* Renders a simple page to display for the theme menu defined above. |
| 201 |
* |
| 202 |
* @since 2.0 |
| 203 |
*/ |
| 204 |
function mtphr_post_duplicator_settings_display() { |
| 205 |
?> |
| 206 |
<div class="wrap"> |
| 207 |
|
| 208 |
<h2><?php _e( 'Post Duplicator Settings', 'post-duplicator' ); ?></h2> |
| 209 |
<?php settings_errors(); ?> |
| 210 |
|
| 211 |
<form method="post" action="options.php"> |
| 212 |
<?php |
| 213 |
settings_fields( 'mtphr_post_duplicator_settings' ); |
| 214 |
do_settings_sections( 'mtphr_post_duplicator_settings' ); |
| 215 |
submit_button(); |
| 216 |
?> |
| 217 |
</form> |
| 218 |
|
| 219 |
</div><!-- /.wrap --> |
| 220 |
<?php |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
/** |
| 227 |
* The callback function for the settings sections. |
| 228 |
* |
| 229 |
* @since 2.0 |
| 230 |
*/ |
| 231 |
function mtphr_post_duplicator_settings_callback() { |
| 232 |
echo '<h4>' . esc_html__( 'Customize the settings for duplicated posts.', 'post-duplicator' ) . '</h4>'; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
/** |
| 239 |
* The custom field callback. |
| 240 |
* |
| 241 |
* @since 2.27 |
| 242 |
*/ |
| 243 |
function mtphr_post_duplicator_field_display( $args ) { |
| 244 |
$value = ''; |
| 245 |
if( isset( $args['default'] ) ) { |
| 246 |
$value = sanitize_text_field( $args['default'] ); |
| 247 |
} |
| 248 |
if( isset($args['type']) ) { |
| 249 |
|
| 250 |
echo '<div class="mtphr-post-duplicator-metaboxer-field mtphr-post-duplicator-metaboxer-' . esc_attr( $args['type'] ) . '">'; |
| 251 |
|
| 252 |
// Call the function to display the field |
| 253 |
if ( function_exists('mtphr_post_duplicator_metaboxer_'. esc_attr( $args['type'] ) ) ) { |
| 254 |
call_user_func( 'mtphr_post_duplicator_metaboxer_'. esc_attr( $args['type'] ), $args, $value ); |
| 255 |
} |
| 256 |
|
| 257 |
echo '<div>'; |
| 258 |
} |
| 259 |
|
| 260 |
// Add a descriptions |
| 261 |
if( isset( $args['description'] ) ) { |
| 262 |
echo '<span class="description"><small>' . wp_kses_post( $args['description'] ) . '</small></span>'; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|