/** * Checks if we are trying to register an already registered post type slug. * * @since 1.3.0 * * @param bool $slug_exists Whether or not the post type slug exists. * @param string $post_type_slug The post type slug being saved. * @param array $post_types Array of CPTUI-registered post types. * * @return bool */ function cptui_check_existing_post_type_slugs($slug_exists = false, $post_type_slug = '', $post_types = array()) { // If true, then we'll already have a conflict, let's not re-process. if (true === $slug_exists) { return $slug_exists; } // Check if CPTUI has already registered this slug. if (array_key_exists(strtolower($post_type_slug), $post_types)) { return true; } // Check if we're registering a reserved post type slug. if (in_array($post_type_slug, cptui_reserved_post_types())) { return true; } // Check if other plugins have registered this same slug. $registered_post_types = get_post_types(array('_builtin' => false, 'public' => false)); if (in_array($post_type_slug, $registered_post_types)) { return true; } // If we're this far, it's false. return $slug_exists; }
/** * Add to or update our CPTUI option with new data. * * @since 1.0.0 * * @param array $data Array of post type data to update. * * @return bool|string False on failure, string on success. */ function cptui_update_post_type($data = array()) { /** * Fires before a post_type is updated to our saved options. * * @since 1.0.0 * * @param array $data Array of post_type data we are updating. */ do_action('cptui_before_update_post_type', $data); # They need to provide a name if (empty($data['cpt_custom_post_type']['name'])) { return cptui_admin_notices('error', '', false, __('Please provide a post type name', 'cpt-plugin')); } # clean up $_POST data foreach ($data as $key => $value) { if (is_string($value)) { $data[$key] = sanitize_text_field($value); } else { array_map('sanitize_text_field', $data[$key]); } } # Check if they didn't put quotes in the name or rewrite slug. if (false !== strpos($data['cpt_custom_post_type']['name'], '\'') || false !== strpos($data['cpt_custom_post_type']['name'], '\\"') || false !== strpos($data['cpt_custom_post_type']['rewrite_slug'], '\'') || false !== strpos($data['cpt_custom_post_type']['rewrite_slug'], '\\"')) { return cptui_admin_notices('error', '', false, __('Please do not use quotes in post type names or rewrite slugs', 'cpt-plugin')); } $post_types = get_option('cptui_post_types', array()); # Check if we already have a post type of that name. if ('new' == $data['cpt_type_status'] && (array_key_exists(strtolower($data['cpt_custom_post_type']['name']), $post_types) || in_array($data['cpt_custom_post_type']['name'], cptui_reserved_post_types()))) { return cptui_admin_notices('error', '', false, sprintf(__('Please choose a different post type name. %s is already registered.', 'cpt-plugin'), $data['cpt_custom_post_type']['name'])); } if (empty($data['cpt_addon_taxes']) || !is_array($data['cpt_addon_taxes'])) { $data['cpt_addon_taxes'] = array(); } if (empty($data['cpt_supports']) || !is_array($data['cpt_supports'])) { $data['cpt_supports'] = array(); } foreach ($data['cpt_labels'] as $key => $label) { if (empty($label)) { unset($data['cpt_labels'][$key]); } $label = str_replace('"', '', htmlspecialchars_decode($label)); $label = htmlspecialchars($label, ENT_QUOTES); $data['cpt_labels'][$key] = stripslashes_deep($label); } if (empty($data['cpt_custom_post_type']['menu_icon'])) { $data['cpt_custom_post_type']['menu_icon'] = null; } $label = str_replace('"', '', htmlspecialchars_decode($data['cpt_custom_post_type']['label'])); $label = htmlspecialchars(stripslashes($label), ENT_QUOTES); $singular_label = str_replace('"', '', htmlspecialchars_decode($data['cpt_custom_post_type']['singular_label'])); $singular_label = htmlspecialchars(stripslashes($singular_label), ENT_QUOTES); $description = stripslashes_deep($data['cpt_custom_post_type']['description']); $post_types[$data['cpt_custom_post_type']['name']] = array('name' => $data['cpt_custom_post_type']['name'], 'label' => $label, 'singular_label' => $singular_label, 'description' => $description, 'public' => disp_boolean($data['cpt_custom_post_type']['public']), 'show_ui' => disp_boolean($data['cpt_custom_post_type']['show_ui']), 'has_archive' => disp_boolean($data['cpt_custom_post_type']['has_archive']), 'has_archive_string' => $data['cpt_custom_post_type']['has_archive_string'], 'exclude_from_search' => disp_boolean($data['cpt_custom_post_type']['exclude_from_search']), 'capability_type' => $data['cpt_custom_post_type']['capability_type'], 'hierarchical' => disp_boolean($data['cpt_custom_post_type']['hierarchical']), 'rewrite' => disp_boolean($data['cpt_custom_post_type']['rewrite']), 'rewrite_slug' => $data['cpt_custom_post_type']['rewrite_slug'], 'rewrite_withfront' => disp_boolean($data['cpt_custom_post_type']['rewrite_withfront']), 'query_var' => disp_boolean($data['cpt_custom_post_type']['query_var']), 'menu_position' => $data['cpt_custom_post_type']['menu_position'], 'show_in_menu' => disp_boolean($data['cpt_custom_post_type']['show_in_menu']), 'show_in_menu_string' => $data['cpt_custom_post_type']['show_in_menu_string'], 'menu_icon' => $data['cpt_custom_post_type']['menu_icon'], 'supports' => $data['cpt_supports'], 'taxonomies' => $data['cpt_addon_taxes'], 'labels' => $data['cpt_labels']); $success = update_option('cptui_post_types', $post_types); /** * Fires after a post type is updated to our saved options. * * @since 1.0.0 * * @param array $data Array of post type data that was updated. */ do_action('cptui_after_update_post_type', $data); flush_rewrite_rules(); if (isset($success)) { if ('new' == $data['cpt_type_status']) { return cptui_admin_notices('add', $data['cpt_custom_post_type']['name'], $success); } } return cptui_admin_notices('update', $data['cpt_custom_post_type']['name'], true); }
/** * Tests our reserved post type utility function. */ public function test_cptui_reserved_post_types() { // Test without filters $reserved = cptui_reserved_post_types(); $defaults = array('post', 'page', 'attachment', 'revision', 'nav_menu_item', 'action', 'order', 'theme'); $this->assertNotEmpty($reserved); foreach ($defaults as $default) { $this->assertContains($default, $reserved); } // Pre filters $this->assertNotContains('reserved_slug', $reserved); $this->assertNotContains('reserved_string_slug', $reserved); // Add filter to get custom slugs array add_filter('cptui_reserved_post_types', array($this, 'add_reserved_post_types_array')); // Fetch new value with the custom slugs added. $reserved = cptui_reserved_post_types(); remove_filter('cptui_reserved_post_types', array($this, 'add_reserved_post_types_array')); // Need to re-test these to make sure they persist. $this->assertNotEmpty($reserved); foreach ($defaults as $default) { $this->assertContains($default, $reserved); } $this->assertContains('reserved_slug', $reserved); // Add filters to get custom slugs string add_filter('cptui_reserved_post_types', array($this, 'add_reserved_post_types_string')); // Fetch new value with the custom slugs added. $reserved = cptui_reserved_post_types(); remove_filter('cptui_reserved_post_types', array($this, 'add_reserved_post_types_string')); // Need to re-test these to make sure they persist. $this->assertNotEmpty($reserved); foreach ($defaults as $default) { $this->assertContains($default, $reserved); } $this->assertContains('reserved_string_slug', $reserved); }