Commit realizado el 12:13:52 08-04-2024
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Category_Taxonomy.
|
||||
*
|
||||
* @link https://github.com/googleforcreators/web-stories-wp
|
||||
*
|
||||
* @copyright 2021 Google LLC
|
||||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Copyright 2021 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Google\Web_Stories\Taxonomy;
|
||||
|
||||
use Google\Web_Stories\REST_API\Stories_Terms_Controller;
|
||||
use Google\Web_Stories\Story_Post_Type;
|
||||
|
||||
/**
|
||||
* Category_Taxonomy class.
|
||||
*
|
||||
* @phpstan-import-type TaxonomyArgs from \Google\Web_Stories\Taxonomy\Taxonomy_Base
|
||||
*/
|
||||
class Category_Taxonomy extends Taxonomy_Base {
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Story_Post_Type $story_post_type Story_Post_Type instance.
|
||||
*/
|
||||
public function __construct( Story_Post_Type $story_post_type ) {
|
||||
$this->taxonomy_post_type = $story_post_type->get_slug();
|
||||
$this->taxonomy_slug = 'web_story_category';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of service IDs required for this service to be registered.
|
||||
*
|
||||
* Needed because the story post type needs to be registered first.
|
||||
*
|
||||
* @since 1.13.0
|
||||
*
|
||||
* @return string[] List of required services.
|
||||
*/
|
||||
public static function get_requirements(): array {
|
||||
return [ 'story_post_type' ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Category args.
|
||||
*
|
||||
* @since 1.12.0
|
||||
*
|
||||
* @return array<string,mixed> Taxonomy args.
|
||||
*
|
||||
* @phpstan-return TaxonomyArgs
|
||||
*/
|
||||
protected function taxonomy_args(): array {
|
||||
$labels = [
|
||||
'name' => _x( 'Categories', 'taxonomy general name', 'web-stories' ),
|
||||
'singular_name' => _x( 'Category', 'taxonomy singular name', 'web-stories' ),
|
||||
'search_items' => __( 'Search Categories', 'web-stories' ),
|
||||
'all_items' => __( 'All Categories', 'web-stories' ),
|
||||
'parent_item' => __( 'Parent Category', 'web-stories' ),
|
||||
'parent_item_colon' => __( 'Parent Category:', 'web-stories' ),
|
||||
'edit_item' => __( 'Edit Category', 'web-stories' ),
|
||||
'view_item' => __( 'View Category', 'web-stories' ),
|
||||
'update_item' => __( 'Update Category', 'web-stories' ),
|
||||
'add_new_item' => __( 'Add New Category', 'web-stories' ),
|
||||
'new_item_name' => __( 'New Category Name', 'web-stories' ),
|
||||
'not_found' => __( 'No categories found.', 'web-stories' ),
|
||||
'no_terms' => __( 'No categories', 'web-stories' ),
|
||||
'filter_by_item' => __( 'Filter by category', 'web-stories' ),
|
||||
'items_list_navigation' => __( 'Categories list navigation', 'web-stories' ),
|
||||
'items_list' => __( 'Categories list', 'web-stories' ),
|
||||
/* translators: Tab heading when selecting from the most used terms. */
|
||||
'most_used' => _x( 'Most Used', 'Categories', 'web-stories' ),
|
||||
'back_to_items' => __( '← Go to Categories', 'web-stories' ),
|
||||
'item_link' => _x( 'Category Link', 'navigation link block title', 'web-stories' ),
|
||||
'item_link_description' => _x( 'A link to a category.', 'navigation link block description', 'web-stories' ),
|
||||
];
|
||||
return [
|
||||
'labels' => $labels,
|
||||
'hierarchical' => true,
|
||||
'public' => false,
|
||||
'publicly_queryable' => true,
|
||||
'show_ui' => true,
|
||||
'show_admin_column' => true,
|
||||
'rewrite' => true,
|
||||
'show_in_rest' => true,
|
||||
'rest_namespace' => self::REST_NAMESPACE,
|
||||
'capabilities' => self::DEFAULT_CAPABILITIES,
|
||||
'rest_controller_class' => Stories_Terms_Controller::class,
|
||||
];
|
||||
}
|
||||
}
|
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Tag_Taxonomy.
|
||||
*
|
||||
* @link https://github.com/googleforcreators/web-stories-wp
|
||||
*
|
||||
* @copyright 2021 Google LLC
|
||||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Copyright 2021 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Google\Web_Stories\Taxonomy;
|
||||
|
||||
use Google\Web_Stories\Infrastructure\HasRequirements;
|
||||
use Google\Web_Stories\REST_API\Stories_Terms_Controller;
|
||||
use Google\Web_Stories\Story_Post_Type;
|
||||
|
||||
/**
|
||||
* Tag_Taxonomy class.
|
||||
*
|
||||
* @phpstan-import-type TaxonomyArgs from \Google\Web_Stories\Taxonomy\Taxonomy_Base
|
||||
*/
|
||||
class Tag_Taxonomy extends Taxonomy_Base implements HasRequirements {
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Story_Post_Type $story_post_type Story_Post_Type instance.
|
||||
*/
|
||||
public function __construct( Story_Post_Type $story_post_type ) {
|
||||
$this->taxonomy_post_type = $story_post_type->get_slug();
|
||||
$this->taxonomy_slug = 'web_story_tag';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of service IDs required for this service to be registered.
|
||||
*
|
||||
* Needed because the story post type needs to be registered first.
|
||||
*
|
||||
* @since 1.13.0
|
||||
*
|
||||
* @return string[] List of required services.
|
||||
*/
|
||||
public static function get_requirements(): array {
|
||||
return [ 'story_post_type' ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Taxonomy args.
|
||||
*
|
||||
* @since 1.12.0
|
||||
*
|
||||
* @return array<string,mixed> Taxonomy args.
|
||||
*
|
||||
* @phpstan-return TaxonomyArgs
|
||||
*/
|
||||
protected function taxonomy_args(): array {
|
||||
$labels = [
|
||||
'name' => _x( 'Tags', 'taxonomy general name', 'web-stories' ),
|
||||
'singular_name' => _x( 'Tag', 'taxonomy singular name', 'web-stories' ),
|
||||
'search_items' => __( 'Search Tags', 'web-stories' ),
|
||||
'popular_items' => __( 'Popular Tags', 'web-stories' ),
|
||||
'all_items' => __( 'All Tags', 'web-stories' ),
|
||||
'edit_item' => __( 'Edit Tag', 'web-stories' ),
|
||||
'view_item' => __( 'View Tag', 'web-stories' ),
|
||||
'update_item' => __( 'Update Tag', 'web-stories' ),
|
||||
'add_new_item' => __( 'Add New Tag', 'web-stories' ),
|
||||
'new_item_name' => __( 'New Tag Name', 'web-stories' ),
|
||||
'separate_items_with_commas' => __( 'Separate tags with commas', 'web-stories' ),
|
||||
'add_or_remove_items' => __( 'Add or remove tags', 'web-stories' ),
|
||||
'choose_from_most_used' => __( 'Choose from the most used tags', 'web-stories' ),
|
||||
'not_found' => __( 'No tags found.', 'web-stories' ),
|
||||
'no_terms' => __( 'No tags', 'web-stories' ),
|
||||
'items_list_navigation' => __( 'Tags list navigation', 'web-stories' ),
|
||||
'items_list' => __( 'Tags list', 'web-stories' ),
|
||||
/* translators: Tab heading when selecting from the most used terms. */
|
||||
'most_used' => _x( 'Most Used', 'Tags', 'web-stories' ),
|
||||
'back_to_items' => __( '← Go to Tags', 'web-stories' ),
|
||||
'item_link' => _x( 'Tag Link', 'navigation link block title', 'web-stories' ),
|
||||
'item_link_description' => _x( 'A link to a tag.', 'navigation link block description', 'web-stories' ),
|
||||
];
|
||||
return [
|
||||
'labels' => $labels,
|
||||
'hierarchical' => false,
|
||||
'public' => false,
|
||||
'publicly_queryable' => true,
|
||||
'show_ui' => true,
|
||||
'show_admin_column' => true,
|
||||
'rewrite' => true,
|
||||
'show_in_rest' => true,
|
||||
'rest_namespace' => self::REST_NAMESPACE,
|
||||
'capabilities' => self::DEFAULT_CAPABILITIES,
|
||||
'rest_controller_class' => Stories_Terms_Controller::class,
|
||||
];
|
||||
}
|
||||
}
|
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Taxonomy_Base.
|
||||
*
|
||||
* @link https://github.com/googleforcreators/web-stories-wp
|
||||
*
|
||||
* @copyright 2021 Google LLC
|
||||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Copyright 2021 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Google\Web_Stories\Taxonomy;
|
||||
|
||||
use Google\Web_Stories\Infrastructure\PluginActivationAware;
|
||||
use Google\Web_Stories\Infrastructure\PluginDeactivationAware;
|
||||
use Google\Web_Stories\Infrastructure\PluginUninstallAware;
|
||||
use Google\Web_Stories\Infrastructure\SiteInitializationAware;
|
||||
use Google\Web_Stories\Service_Base;
|
||||
use WP_Site;
|
||||
use WP_Term;
|
||||
use WP_Term_Query;
|
||||
|
||||
/**
|
||||
* Taxonomy_Base class
|
||||
*
|
||||
* @phpstan-type TaxonomyArgs array{
|
||||
* labels?: string[],
|
||||
* description?: string,
|
||||
* public?: bool,
|
||||
* publicly_queryable?: bool,
|
||||
* hierarchical?: bool,
|
||||
* show_ui?: bool,
|
||||
* show_in_menu?: bool,
|
||||
* show_in_nav_menus?: bool,
|
||||
* show_in_rest?: bool,
|
||||
* rest_base?: string,
|
||||
* rest_namespace?: string,
|
||||
* rest_controller_class?: string,
|
||||
* show_tagcloud?: bool,
|
||||
* show_in_quick_edit?: bool,
|
||||
* show_admin_column?: bool,
|
||||
* meta_box_cb?: bool|callable,
|
||||
* meta_box_sanitize_cb?: callable,
|
||||
* capabilities?: string[],
|
||||
* rewrite?: bool|array{
|
||||
* slug?: string,
|
||||
* with_front?: bool,
|
||||
* hierarchical?: bool,
|
||||
* ep_mask?: int
|
||||
* },
|
||||
* query_var?: string|bool,
|
||||
* update_count_callback?: callable,
|
||||
* default_term?: string|array{
|
||||
* name?: string,
|
||||
* slug?: string,
|
||||
* description?: string
|
||||
* },
|
||||
* sort?: bool,
|
||||
* args?: array<string, mixed>,
|
||||
* _builtin?: bool,
|
||||
* }
|
||||
*/
|
||||
abstract class Taxonomy_Base extends Service_Base implements PluginActivationAware, PluginDeactivationAware, SiteInitializationAware, PluginUninstallAware {
|
||||
|
||||
public const DEFAULT_CAPABILITIES = [
|
||||
'manage_terms' => 'manage_terms_web-stories',
|
||||
'edit_terms' => 'edit_terms_web-stories',
|
||||
'delete_terms' => 'delete_terms_web-stories',
|
||||
'assign_terms' => 'assign_terms_web-stories',
|
||||
];
|
||||
|
||||
/**
|
||||
* Default REST Namespace.
|
||||
*/
|
||||
public const REST_NAMESPACE = 'web-stories/v1';
|
||||
|
||||
/**
|
||||
* Taxonomy key, must not exceed 32 characters.
|
||||
*/
|
||||
protected string $taxonomy_slug;
|
||||
|
||||
/**
|
||||
* Object type which the taxonomy should be associated.
|
||||
*/
|
||||
protected string $taxonomy_post_type;
|
||||
|
||||
/**
|
||||
* Register taxonomy on register service.
|
||||
*
|
||||
* @since 1.12.0
|
||||
*/
|
||||
public function register(): void {
|
||||
$this->register_taxonomy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register taxonomy.
|
||||
*
|
||||
* @since 1.12.0
|
||||
*/
|
||||
public function register_taxonomy(): void {
|
||||
register_taxonomy( $this->taxonomy_slug, $this->taxonomy_post_type, $this->taxonomy_args() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister taxonomy.
|
||||
*
|
||||
* @since 1.12.0
|
||||
*/
|
||||
public function unregister_taxonomy(): void {
|
||||
unregister_taxonomy( $this->taxonomy_slug );
|
||||
}
|
||||
|
||||
/**
|
||||
* Act on site initialization.
|
||||
*
|
||||
* @since 1.12.0
|
||||
*
|
||||
* @param WP_Site $site The site being initialized.
|
||||
*/
|
||||
public function on_site_initialization( WP_Site $site ): void {
|
||||
$this->register_taxonomy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Act on plugin activation.
|
||||
*
|
||||
* @since 1.12.0
|
||||
*
|
||||
* @param bool $network_wide Whether the activation was done network-wide.
|
||||
*/
|
||||
public function on_plugin_activation( bool $network_wide ): void {
|
||||
$this->register_taxonomy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Act on plugin deactivation.
|
||||
*
|
||||
* @since 1.12.0
|
||||
*
|
||||
* @param bool $network_wide Whether the deactivation was done network-wide.
|
||||
*/
|
||||
public function on_plugin_deactivation( bool $network_wide ): void {
|
||||
$this->unregister_taxonomy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get taxonomy slug.
|
||||
*
|
||||
* @since 1.12.0
|
||||
*/
|
||||
public function get_taxonomy_slug(): string {
|
||||
return $this->taxonomy_slug;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Act on plugin uninstall.
|
||||
*
|
||||
* @since 1.26.0
|
||||
*/
|
||||
public function on_plugin_uninstall(): void {
|
||||
clean_taxonomy_cache( $this->get_taxonomy_slug() );
|
||||
|
||||
$term_query = new WP_Term_Query();
|
||||
$terms = $term_query->query(
|
||||
[
|
||||
'taxonomy' => $this->get_taxonomy_slug(),
|
||||
'hide_empty' => false,
|
||||
]
|
||||
);
|
||||
|
||||
if ( empty( $terms ) || ! \is_array( $terms ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $terms as $term ) {
|
||||
if ( $term instanceof WP_Term ) {
|
||||
wp_delete_term( $term->term_id, $term->taxonomy );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Taxonomy args.
|
||||
*
|
||||
* @since 1.12.0
|
||||
*
|
||||
* @return TaxonomyArgs Taxonomy args.
|
||||
*/
|
||||
abstract protected function taxonomy_args(): array;
|
||||
}
|
Reference in New Issue
Block a user