Admin.php
6 years ago
FlushRules.php
8 years ago
GetArchives.php
6 years ago
Option.php
6 years ago
Permalink.php
6 years ago
Rewrite.php
6 years ago
Setting.php
6 years ago
Admin.php
222 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Admin Page View. |
| 4 | * |
| 5 | * @package Custom_Post_Type_Permalinks |
| 6 | * @since 0.9.4 |
| 7 | * */ |
| 8 | |
| 9 | /** |
| 10 | * Admin Page Class. |
| 11 | * |
| 12 | * @since 0.9.4 |
| 13 | * */ |
| 14 | class CPTP_Module_Admin extends CPTP_Module { |
| 15 | |
| 16 | /** |
| 17 | * Add actions. |
| 18 | */ |
| 19 | public function add_hook() { |
| 20 | add_action( 'admin_init', array( $this, 'settings_api_init' ), 30 ); |
| 21 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_css_js' ) ); |
| 22 | add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Setting Init |
| 27 | * |
| 28 | * @since 0.7 |
| 29 | */ |
| 30 | public function settings_api_init() { |
| 31 | add_settings_section( |
| 32 | 'cptp_setting_section', |
| 33 | __( 'Permalink Settings for Custom Post Types', 'custom-post-type-permalinks' ), |
| 34 | array( $this, 'setting_section_callback_function' ), |
| 35 | 'permalink' |
| 36 | ); |
| 37 | |
| 38 | $post_types = CPTP_Util::get_post_types(); |
| 39 | |
| 40 | foreach ( $post_types as $post_type ) { |
| 41 | add_settings_field( |
| 42 | $post_type . '_structure', |
| 43 | $post_type, |
| 44 | array( $this, 'setting_structure_callback_function' ), |
| 45 | 'permalink', |
| 46 | 'cptp_setting_section', |
| 47 | array( |
| 48 | 'label_for' => $post_type . '_structure', |
| 49 | 'post_type' => $post_type, |
| 50 | ) |
| 51 | ); |
| 52 | register_setting( 'permalink', $post_type . '_structure' ); |
| 53 | } |
| 54 | |
| 55 | add_settings_field( |
| 56 | 'no_taxonomy_structure', |
| 57 | __( 'Use custom permalink of custom taxonomy archive.', 'custom-post-type-permalinks' ), |
| 58 | array( $this, 'setting_no_tax_structure_callback_function' ), |
| 59 | 'permalink', |
| 60 | 'cptp_setting_section', |
| 61 | array( |
| 62 | 'label_for' => 'no_taxonomy_structure', |
| 63 | ) |
| 64 | ); |
| 65 | |
| 66 | register_setting( 'permalink', 'no_taxonomy_structure' ); |
| 67 | |
| 68 | add_settings_field( |
| 69 | 'add_post_type_for_tax', |
| 70 | __( 'Add <code>post_type</code> query for custom taxonomy archive.', 'custom-post-type-permalinks' ), |
| 71 | array( $this, 'add_post_type_for_tax_callback_function' ), |
| 72 | 'permalink', |
| 73 | 'cptp_setting_section', |
| 74 | array( |
| 75 | 'label_for' => 'add_post_type_for_tax', |
| 76 | ) |
| 77 | ); |
| 78 | |
| 79 | register_setting( 'permalink', 'no_taxonomy_structure' ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Setting section view. |
| 84 | */ |
| 85 | public function setting_section_callback_function() { |
| 86 | $sptp_link = admin_url( 'plugin-install.php?s=simple-post-type-permalinks&tab=search&type=term' ); |
| 87 | // translators: %s simple post type permalinks install page. |
| 88 | $sptp_template = __( 'If you need post type permalink only, you should use <a href="%s">Simple Post Type Permalinks</a>.', 'custom-post-type-permalinks' ); |
| 89 | ?> |
| 90 | <p> |
| 91 | <strong> |
| 92 | <?php |
| 93 | $allowed_html = array( |
| 94 | 'a' => array( |
| 95 | 'href' => true, |
| 96 | ), |
| 97 | ); |
| 98 | echo wp_kses( sprintf( $sptp_template, esc_url( $sptp_link ) ), $allowed_html ); |
| 99 | ?> |
| 100 | </strong> |
| 101 | </p> |
| 102 | <?php |
| 103 | $allowed_html_code_tag = array( |
| 104 | 'code' => array(), |
| 105 | ); |
| 106 | ?> |
| 107 | |
| 108 | <p><?php echo wp_kses( __( 'The tags you can use are WordPress Structure Tags and <code>%"custom_taxonomy_slug"%</code> (e.g. <code>%actors%</code> or <code>%movie_actors%</code>).', 'custom-post-type-permalinks' ), $allowed_html_code_tag ); ?> |
| 109 | <?php echo wp_kses( __( '<code>%"custom_taxonomy_slug"%</code> is replaced by the term of taxonomy.', 'custom-post-type-permalinks' ), $allowed_html_code_tag ); ?></p> |
| 110 | |
| 111 | <p><?php esc_html_e( "Presence of the trailing '/' is unified into a standard permalink structure setting.", 'custom-post-type-permalinks' ); ?> |
| 112 | <p><?php echo wp_kses( __( 'If <code>has_archive</code> is true, add permalinks for custom post type archive.', 'custom-post-type-permalinks' ), $allowed_html_code_tag ); ?></p> |
| 113 | |
| 114 | <?php |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Show setting structure input. |
| 119 | * |
| 120 | * @param array $option { |
| 121 | * Callback option. |
| 122 | * @type string 'post_type' post type name. |
| 123 | * @type string 'label_for' post type label. |
| 124 | * } |
| 125 | */ |
| 126 | public function setting_structure_callback_function( $option ) { |
| 127 | $post_type = $option['post_type']; |
| 128 | $name = $option['label_for']; |
| 129 | $pt_object = get_post_type_object( $post_type ); |
| 130 | $slug = $pt_object->rewrite['slug']; |
| 131 | $with_front = $pt_object->rewrite['with_front']; |
| 132 | |
| 133 | $value = CPTP_Util::get_permalink_structure( $post_type ); |
| 134 | |
| 135 | $disabled = false; |
| 136 | if ( isset( $pt_object->cptp_permalink_structure ) && $pt_object->cptp_permalink_structure ) { |
| 137 | $disabled = true; |
| 138 | } |
| 139 | |
| 140 | if ( ! $value ) { |
| 141 | $value = CPTP_DEFAULT_PERMALINK; |
| 142 | } |
| 143 | |
| 144 | global $wp_rewrite; |
| 145 | $front = substr( $wp_rewrite->front, 1 ); |
| 146 | if ( $front && $with_front ) { |
| 147 | $slug = $front . $slug; |
| 148 | } |
| 149 | ?> |
| 150 | <p> |
| 151 | <code><?php echo esc_html( home_url() . ( $slug ? '/' : '' ) . $slug ); ?></code> |
| 152 | <input name="<?php echo esc_attr( $name ); ?>" id="<?php echo esc_attr( $name ); ?>" type="text" class="regular-text code " value="<?php echo esc_attr( $value ); ?>" <?php disabled( $disabled, true, true ); ?> /> |
| 153 | </p> |
| 154 | <p>has_archive: <code><?php echo esc_html( $pt_object->has_archive ? 'true' : 'false' ); ?></code> / with_front: |
| 155 | <code><?php echo esc_html( $pt_object->rewrite['with_front'] ? 'true' : 'false' ); ?></code></p> |
| 156 | <?php |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Show checkbox no tax. |
| 161 | */ |
| 162 | public function setting_no_tax_structure_callback_function() { |
| 163 | $no_taxonomy_structure = CPTP_Util::get_no_taxonomy_structure(); |
| 164 | echo '<input name="no_taxonomy_structure" id="no_taxonomy_structure" type="checkbox" value="1" class="code" ' . checked( false, $no_taxonomy_structure, false ) . ' /> '; |
| 165 | $txt = __( "If you check this, the custom taxonomy's permalinks will be <code>%s/post_type/taxonomy/term</code>.", 'custom-post-type-permalinks' ); |
| 166 | echo sprintf( wp_kses( $txt, array( 'code' => array() ) ), esc_html( home_url() ) ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Show checkbox for post type query. |
| 171 | */ |
| 172 | public function add_post_type_for_tax_callback_function() { |
| 173 | echo '<input name="add_post_type_for_tax" id="add_post_type_for_tax" type="checkbox" value="1" class="code" ' . checked( true, get_option( 'add_post_type_for_tax' ), false ) . ' /> '; |
| 174 | esc_html_e( 'Custom taxonomy archive also works as post type archive. ', 'custom-post-type-permalinks' ); |
| 175 | esc_html_e( 'There are cases when the template to be loaded is changed.', 'custom-post-type-permalinks' ); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /** |
| 180 | * Enqueue css and js |
| 181 | * |
| 182 | * @since 0.8.5 |
| 183 | */ |
| 184 | public function enqueue_css_js() { |
| 185 | $pointer_name = 'custom-post-type-permalinks-settings'; |
| 186 | if ( ! is_network_admin() ) { |
| 187 | $dismissed = explode( ',', get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ); |
| 188 | if ( false === array_search( $pointer_name, $dismissed, true ) ) { |
| 189 | $content = ''; |
| 190 | $content .= '<h3>' . __( 'Custom Post Type Permalinks', 'custom-post-type-permalinks' ) . '</h3>'; |
| 191 | $content .= '<p>' . __( 'You can setting permalink for post type in <a href="options-permalink.php">Permalinks</a>.', 'custom-post-type-permalinks' ) . '</p>'; |
| 192 | |
| 193 | wp_enqueue_style( 'wp-pointer' ); |
| 194 | wp_enqueue_script( 'wp-pointer' ); |
| 195 | wp_enqueue_script( 'custom-post-type-permalinks-pointer', plugins_url( 'assets/settings-pointer.js', CPTP_PLUGIN_FILE ), array( 'wp-pointer' ), CPTP_VERSION ); |
| 196 | |
| 197 | wp_localize_script( |
| 198 | 'custom-post-type-permalinks-pointer', |
| 199 | 'CPTP_Settings_Pointer', |
| 200 | array( |
| 201 | 'content' => $content, |
| 202 | 'name' => $pointer_name, |
| 203 | ) |
| 204 | ); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Admin notice for update permalink settings! |
| 211 | */ |
| 212 | public function admin_notices() { |
| 213 | if ( version_compare( get_option( 'cptp_permalink_checked' ), '3.0.0', '<' ) ) { |
| 214 | // translators: %s URL. |
| 215 | $format = __( '[Custom Post Type Permalinks] <a href="%s"><strong>Please check your permalink settings!</strong></a>', 'custom-post-type-permalinks' ); |
| 216 | $message = sprintf( $format, admin_url( 'options-permalink.php' ) ); |
| 217 | echo sprintf( '<div class="notice notice-warning"><p>%s</p></div>', wp_kses( $message, wp_kses_allowed_html( 'post' ) ) ); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 |