show_ui && 'attachment' !== $type && current_user_can( $object->cap->edit_others_posts ); } /** * Button above a post list, only for users who may sort this type. * * @param mixed $type Post type of the list (WordPress passes a string). */ public static function entry( $type = 'post' ): void { if ( ! is_string( $type ) || ! self::can_sort_type( $type ) ) { return; } echo '' . esc_html__( 'Sort order', '3task-post-order' ) . ' '; } /** * Row action for users who may sort this item. * * @param array $actions Existing actions. * @param \WP_Post $post Row item. * @return array */ public static function row( array $actions, $post ): array { if ( self::can_sort_type( $post->post_type ) && current_user_can( 'edit_post', $post->ID ) ) { $url = add_query_arg( array( 'page' => 'threepo', 'object' => $post->ID, ), admin_url( 'tools.php' ) ); $actions['threepo'] = '' . esc_html__( 'Order', '3task-post-order' ) . ''; } return $actions; } /** * Asset version: plugin version plus file time, so a changed file always gets a new address and no browser keeps an old copy. */ public static function ver( string $file ): string { $time = @filemtime( plugin_dir_path( THREEPO_FILE ) . $file ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- A missing file only drops the time part. return THREEPO_VERSION . ( $time ? '.' . $time : '' ); } public static function assets( string $hook ): void { if ( 'tools_page_threepo' !== $hook ) { return; } wp_enqueue_style( 'threepo-admin', plugins_url( 'assets/admin.css', THREEPO_FILE ), array(), self::ver( 'assets/admin.css' ) ); wp_enqueue_script( 'threepo-admin', plugins_url( 'assets/admin.js', THREEPO_FILE ), array( 'wp-api-fetch', 'wp-i18n' ), self::ver( 'assets/admin.js' ), true ); wp_add_inline_script( 'threepo-admin', 'window.threepoIcons = ' . wp_json_encode( Icons::paths() ) . ';', 'before' ); wp_set_script_translations( 'threepo-admin', '3task-post-order' ); } public static function editor(): void { wp_enqueue_script( 'threepo-editor', plugins_url( 'assets/editor.js', THREEPO_FILE ), array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-compose', 'wp-hooks', 'wp-block-editor', 'wp-api-fetch', 'wp-i18n' ), self::ver( 'assets/editor.js' ), true ); wp_set_script_translations( 'threepo-editor', '3task-post-order' ); } public static function page(): void { if ( ! current_user_can( 'edit_posts' ) ) { return; } $manage = current_user_can( 'manage_options' ); ?> 'GET', 'permission_callback' => array( Api::class, 'logged' ), 'callback' => array( self::class, 'catalog' ), ) ); register_rest_route( 'threepo/v1', '/lookup', array( 'methods' => 'GET', 'permission_callback' => array( Api::class, 'logged' ), 'callback' => array( self::class, 'lookup' ), 'args' => array( 'kind' => array( 'type' => 'string', 'enum' => array( 'post', 'term' ), 'required' => true, ), 'type' => array( 'type' => 'string', 'maxLength' => 32, 'required' => true, ), 'search' => array( 'type' => 'string', 'maxLength' => 200, 'default' => '', ), ), ) ); } /** * @return array */ public static function catalog(): array { $types = array(); foreach ( get_post_types( array( 'show_ui' => true ), 'objects' ) as $o ) { if ( 'attachment' === $o->name || ! current_user_can( $o->cap->edit_others_posts ) ) { continue; } $taxes = array(); foreach ( get_object_taxonomies( $o->name, 'objects' ) as $t ) { if ( $t->show_ui ) { $taxes[] = array( 'name' => $t->name, 'label' => $t->label, ); } } $types[] = array( 'name' => $o->name, 'label' => $o->label, 'hierarchical' => $o->hierarchical, 'taxonomies' => $taxes, ); } $taxes = array(); foreach ( get_taxonomies( array( 'show_ui' => true ), 'objects' ) as $t ) { if ( current_user_can( $t->cap->manage_terms ) ) { $taxes[] = array( 'name' => $t->name, 'label' => $t->label, 'hierarchical' => $t->hierarchical, ); } } return array( 'types' => $types, 'taxonomies' => $taxes, 'languages_supported' => ! ( defined( 'ICL_SITEPRESS_VERSION' ) || function_exists( 'pll_languages_list' ) ), ); } /** * @return list>|\WP_Error */ public static function lookup( WP_REST_Request $r ) { $items = array(); $type = $r['type']; if ( 'term' === $r['kind'] ) { $tax = get_taxonomy( $type ); if ( ! $tax || ! current_user_can( $tax->cap->manage_terms ) ) { return Store::error( 'forbidden', __( 'This taxonomy is unavailable.', '3task-post-order' ), 403 ); } $terms = get_terms( array( 'taxonomy' => $type, 'hide_empty' => false, 'number' => 50, 'search' => $r['search'], ) ); if ( is_wp_error( $terms ) ) { return $terms; } foreach ( $terms as $term ) { $items[] = array( 'id' => $term->term_id, 'title' => $term->name, ); } return $items; } $o = get_post_type_object( $type ); if ( ! $o || ! current_user_can( $o->cap->edit_others_posts ) ) { return Store::error( 'forbidden', __( 'This content type is unavailable.', '3task-post-order' ), 403 ); } $posts = get_posts( array( 'post_type' => $type, 'post_status' => 'publish', 'posts_per_page' => 50, 's' => $r['search'], 'suppress_filters' => false, ) ); foreach ( $posts as $post ) { if ( current_user_can( 'edit_post', $post->ID ) ) { $items[] = array( 'id' => $post->ID, 'title' => wp_strip_all_tags( $post->post_title ), ); } } return $items; } }