From 07bfba55ff73aadd6b0f33487f803c40a77d4801 Mon Sep 17 00:00:00 2001 From: Mehdi Rezaei Date: Fri, 4 Oct 2024 13:38:34 +0330 Subject: [PATCH] add link to columns bug fix change namespace for wordpress bug fix variable issue --- README.md | 2 +- custom-api-creator.php | 208 +++++++++++++++++++++-------------------- readme.txt | 2 +- 3 files changed, 109 insertions(+), 103 deletions(-) diff --git a/README.md b/README.md index af8f211..b7a9b95 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ **Tags:** api, rest api, custom api, api builder, wp json **Requires at least:** 5.0 **Tested up to:** 6.6.1 -**Stable tag:** 1.0.2 +**Stable tag:** 1.0.3 **Requires PHP:** 7.0 **License:** GPLv2 or later **License URI:** https://www.gnu.org/licenses/gpl-2.0.html diff --git a/custom-api-creator.php b/custom-api-creator.php index 67b9da0..79eb78d 100644 --- a/custom-api-creator.php +++ b/custom-api-creator.php @@ -3,12 +3,12 @@ * Plugin Name: Custom API Creator * Plugin URI: https://github.com/mehdiraized/wp-custom-api-creator * Description: Create custom APIs with flexible output and user roles. - * Version: 1.0.2 + * Version: 1.0.3 * Author: Mehdi Rezaei * Author URI: https://mehd.ir * License: GPLv2 or later * License URI: https://www.gnu.org/licenses/gpl-2.0.html - * Text Domain: custom-api-creator + * Text Domain: cac-plugin-creator * Domain Path: /languages */ @@ -16,37 +16,37 @@ exit; // Exit if accessed directly } -class Custom_API_Creator { +class CAC_Plugin_Class { public function __construct() { add_action( 'init', array( $this, 'register_custom_post_type' ) ); add_action( 'init', array( $this, 'load_textdomain' ) ); add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); - add_action( 'rest_api_init', array( $this, 'register_custom_apis' ) ); - add_action( 'add_meta_boxes', array( $this, 'add_custom_api_meta_boxes' ) ); - add_action( 'save_post', array( $this, 'save_custom_api_meta' ) ); + add_action( 'rest_api_init', array( $this, 'register_cac_plugins' ) ); + add_action( 'add_meta_boxes', array( $this, 'add_cac_plugin_meta_boxes' ) ); + add_action( 'save_post', array( $this, 'save_cac_plugin_meta' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); // custom column - add_filter( 'manage_custom_api_posts_columns', array( $this, 'add_custom_columns' ) ); - add_action( 'manage_custom_api_posts_custom_column', array( $this, 'custom_column_content' ), 10, 2 ); + add_filter( 'manage_cac_plugin_posts_columns', array( $this, 'add_custom_columns' ) ); + add_action( 'manage_cac_plugin_posts_custom_column', array( $this, 'custom_column_content' ), 10, 2 ); } public function register_custom_post_type() { $labels = array( - 'name' => _x( 'Custom APIs', 'post type general name', 'custom-api-creator' ), - 'singular_name' => _x( 'Custom API', 'post type singular name', 'custom-api-creator' ), - 'menu_name' => _x( 'Custom APIs', 'admin menu', 'custom-api-creator' ), - 'name_admin_bar' => _x( 'Custom API', 'add new on admin bar', 'custom-api-creator' ), - 'add_new' => _x( 'Add New', 'custom api', 'custom-api-creator' ), - 'add_new_item' => __( 'Add New API', 'custom-api-creator' ), - 'new_item' => __( 'New API', 'custom-api-creator' ), - 'edit_item' => __( 'Edit API', 'custom-api-creator' ), - 'view_item' => __( 'View API', 'custom-api-creator' ), - 'all_items' => __( 'All APIs', 'custom-api-creator' ), - 'search_items' => __( 'Search API', 'custom-api-creator' ), - 'parent_item_colon' => __( 'Parent APIs:', 'custom-api-creator' ), - 'not_found' => __( 'No apis found.', 'custom-api-creator' ), - 'not_found_in_trash' => __( 'No apis found in Trash.', 'custom-api-creator' ) + 'name' => _x( 'Custom APIs', 'post type general name', 'cac-plugin-creator' ), + 'singular_name' => _x( 'Custom API', 'post type singular name', 'cac-plugin-creator' ), + 'menu_name' => _x( 'Custom APIs', 'admin menu', 'cac-plugin-creator' ), + 'name_admin_bar' => _x( 'Custom API', 'add new on admin bar', 'cac-plugin-creator' ), + 'add_new' => _x( 'Add New', 'custom api', 'cac-plugin-creator' ), + 'add_new_item' => __( 'Add New API', 'cac-plugin-creator' ), + 'new_item' => __( 'New API', 'cac-plugin-creator' ), + 'edit_item' => __( 'Edit API', 'cac-plugin-creator' ), + 'view_item' => __( 'View API', 'cac-plugin-creator' ), + 'all_items' => __( 'All APIs', 'cac-plugin-creator' ), + 'search_items' => __( 'Search API', 'cac-plugin-creator' ), + 'parent_item_colon' => __( 'Parent APIs:', 'cac-plugin-creator' ), + 'not_found' => __( 'No apis found.', 'cac-plugin-creator' ), + 'not_found_in_trash' => __( 'No apis found in Trash.', 'cac-plugin-creator' ) ); $args = array( @@ -56,7 +56,7 @@ public function register_custom_post_type() { 'show_ui' => true, 'show_in_menu' => false, 'query_var' => true, - 'rewrite' => array( 'slug' => 'custom-api' ), + 'rewrite' => array( 'slug' => 'cac-plugin' ), 'capability_type' => 'post', 'has_archive' => false, 'hierarchical' => false, @@ -64,19 +64,19 @@ public function register_custom_post_type() { 'supports' => array( 'title' ) ); - register_post_type( 'custom_api', $args ); + register_post_type( 'cac_plugin', $args ); } public function load_textdomain() { - load_plugin_textdomain( 'custom-api-creator', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); + load_plugin_textdomain( 'cac-plugin-creator', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); } public function add_admin_menu() { add_menu_page( - __( 'Custom API', 'custom-api-creator' ), - __( 'Custom API', 'custom-api-creator' ), + __( 'Custom API', 'cac-plugin-creator' ), + __( 'Custom API', 'cac-plugin-creator' ), 'manage_options', - 'edit.php?post_type=custom_api', + 'edit.php?post_type=cac_plugin', null, 'dashicons-rest-api', 30 @@ -89,31 +89,31 @@ public function enqueue_admin_scripts( $hook ) { } global $post; - if ( 'custom_api' !== $post->post_type ) { + if ( 'cac_plugin' !== $post->post_type ) { return; } - wp_enqueue_script( 'custom-api-admin', plugin_dir_url( __FILE__ ) . 'assets/js/script.js', array( 'jquery' ), '1.0', true ); + wp_enqueue_script( 'cac-plugin-admin', plugin_dir_url( __FILE__ ) . 'assets/js/script.js', array( 'jquery' ), '1.0', true ); } - public function add_custom_api_meta_boxes() { + public function add_cac_plugin_meta_boxes() { add_meta_box( - 'custom_api_details', - __( 'API Details', 'custom-api-creator' ), + 'cac_plugin_details', + __( 'API Details', 'cac-plugin-creator' ), array( $this, 'render_api_details_meta_box' ), - 'custom_api', + 'cac_plugin', 'normal', 'high' ); } public function render_api_details_meta_box( $post ) { - wp_nonce_field( 'custom_api_meta_box', 'custom_api_meta_box_nonce' ); + wp_nonce_field( 'cac_plugin_meta_box', 'cac_plugin_meta_box_nonce' ); - $endpoint = get_post_meta( $post->ID, '_custom_api_endpoint', true ); - $sections = get_post_meta( $post->ID, '_custom_api_sections', true ); - $access_type = get_post_meta( $post->ID, '_custom_api_access_type', true ) ?: 'public'; - $roles = get_post_meta( $post->ID, '_custom_api_roles', true ) ?: array(); + $endpoint = get_post_meta( $post->ID, '_cac_plugin_endpoint', true ); + $sections = get_post_meta( $post->ID, '_cac_plugin_sections', true ); + $access_type = get_post_meta( $post->ID, '_cac_plugin_access_type', true ) ?: 'public'; + $roles = get_post_meta( $post->ID, '_cac_plugin_roles', true ) ?: array(); $post_types = get_post_types( array( 'public' => true ), 'objects' ); $all_roles = wp_roles()->get_names(); @@ -122,15 +122,15 @@ public function render_api_details_meta_box( $post ) { + for="cac_plugin_endpoint"> - + - + - - + + @@ -182,18 +182,18 @@ private function render_section_fields( $post_types, $all_taxonomies, $index, $s $section = $section ?: array( 'name' => '1', 'post_type' => '', 'fields' => array(), 'taxonomies' => array() ); ?>
-

+

-
+
-

-
+
name, $section['taxonomies'] ); ?> -
+ label ); ?> +

0 ) : ?> + class="button remove-section">
'custom_api_endpoint', - '_custom_api_sections' => 'custom_api_sections', - '_custom_api_access_type' => 'custom_api_access_type', - '_custom_api_roles' => 'custom_api_roles' + '_cac_plugin_endpoint' => 'cac_plugin_endpoint', + '_cac_plugin_sections' => 'cac_plugin_sections', + '_cac_plugin_access_type' => 'cac_plugin_access_type', + '_cac_plugin_roles' => 'cac_plugin_roles' ); foreach ( $fields as $meta_key => $post_key ) { if ( isset( $_POST[ $post_key ] ) ) { - $value = $post_key === 'custom_api_sections' ? $this->sanitize_sections( sanitize_text_field( wp_unslash( $_POST[ $post_key ] ) ) ) : $this->sanitize_array_or_string( sanitize_text_field( wp_unslash( $_POST[ $post_key ] ) ) ); + $value = $post_key === 'cac_plugin_sections' ? $this->sanitize_sections( $_POST[ $post_key ] ) : $this->sanitize_array_or_string( $_POST[ $post_key ] ); update_post_meta( $post_id, $meta_key, $value ); } else { delete_post_meta( $post_id, $meta_key ); @@ -268,12 +271,14 @@ public function save_custom_api_meta( $post_id ) { private function sanitize_sections( $sections ) { $sanitized = array(); + if ( ! is_array( $sections ) ) + return null; foreach ( $sections as $index => $section ) { $sanitized[ $index ] = array( - 'name' => sanitize_text_field( $section['name'] ), - 'post_type' => sanitize_text_field( $section['post_type'] ), - 'fields' => isset( $section['fields'] ) ? array_map( 'sanitize_text_field', $section['fields'] ) : array(), - 'taxonomies' => isset( $section['taxonomies'] ) ? array_map( 'sanitize_text_field', $section['taxonomies'] ) : array() + 'name' => sanitize_text_field( wp_unslash( $section['name'] ) ), + 'post_type' => sanitize_text_field( wp_unslash( $section['post_type'] ) ), + 'fields' => isset( $section['fields'] ) ? array_map( 'sanitize_text_field', wp_unslash( $section['fields'] ) ) : array(), + 'taxonomies' => isset( $section['taxonomies'] ) ? array_map( 'sanitize_text_field', wp_unslash( $section['taxonomies'] ) ) : array() ); } return $sanitized; @@ -286,17 +291,17 @@ private function sanitize_array_or_string( $data ) { return sanitize_text_field( $data ); } - public function register_custom_apis() { - $custom_apis = get_posts( array( - 'post_type' => 'custom_api', + public function register_cac_plugins() { + $cac_plugins = get_posts( array( + 'post_type' => 'cac_plugin', 'posts_per_page' => -1, ) ); - foreach ( $custom_apis as $api ) { - $endpoint = get_post_meta( $api->ID, '_custom_api_endpoint', true ); - $roles = get_post_meta( $api->ID, '_custom_api_roles', true ); + foreach ( $cac_plugins as $api ) { + $endpoint = get_post_meta( $api->ID, '_cac_plugin_endpoint', true ); + $roles = get_post_meta( $api->ID, '_cac_plugin_roles', true ); - register_rest_route( 'custom-api/v1', '/' . ltrim( $endpoint, '/' ), array( + register_rest_route( 'cac-plugin/v1', '/' . ltrim( $endpoint, '/' ), array( 'methods' => 'GET', 'callback' => array( $this, 'handle_api_request' ), 'permission_callback' => function () use ($roles) { @@ -310,19 +315,19 @@ public function handle_api_request( $request ) { $params = $request->get_params(); $endpoint = $request->get_route(); - $api_post = $this->get_api_by_endpoint( substr( $endpoint, strlen( '/custom-api/v1/' ) ) ); + $api_post = $this->get_api_by_endpoint( substr( $endpoint, strlen( '/cac-plugin/v1/' ) ) ); if ( ! $api_post ) { return new WP_Error( 'invalid_api', 'Invalid API endpoint', array( 'status' => 404 ) ); } - $access_type = get_post_meta( $api_post->ID, '_custom_api_access_type', true ); - $roles = get_post_meta( $api_post->ID, '_custom_api_roles', true ); + $access_type = get_post_meta( $api_post->ID, '_cac_plugin_access_type', true ); + $roles = get_post_meta( $api_post->ID, '_cac_plugin_roles', true ); if ( $access_type === 'private' && ! $this->check_api_permissions( $roles ) ) { return new WP_Error( 'unauthorized', 'You do not have permission to access this API', array( 'status' => 403 ) ); } - $sections = get_post_meta( $api_post->ID, '_custom_api_sections', true ); + $sections = get_post_meta( $api_post->ID, '_cac_plugin_sections', true ); $response = array(); foreach ( $sections as $section ) { @@ -336,21 +341,21 @@ public function handle_api_request( $request ) { } private function get_api_by_endpoint( $endpoint ) { - $endpoint = str_replace( 'custom-api/v1/', '', $endpoint ); + $endpoint = str_replace( 'cac-plugin/v1/', '', $endpoint ); - $custom_apis = get_posts( array( - 'post_type' => 'custom_api', + $cac_plugins = get_posts( array( + 'post_type' => 'cac_plugin', 'posts_per_page' => 1, 'meta_query' => array( array( - 'key' => '_custom_api_endpoint', + 'key' => '_cac_plugin_endpoint', 'value' => $endpoint, 'compare' => '=' ) ) ) ); - return ! empty( $custom_apis ) ? $custom_apis[0] : null; + return ! empty( $cac_plugins ) ? $cac_plugins[0] : null; } private function check_api_permissions( $allowed_roles ) { @@ -434,8 +439,8 @@ public function add_custom_columns( $columns ) { foreach ( $columns as $key => $value ) { $new_columns[ $key ] = $value; if ( $key === 'title' ) { - $new_columns['endpoint'] = __( 'Endpoint', 'custom-api-creator' ); - $new_columns['permission'] = __( 'Permission', 'custom-api-creator' ); + $new_columns['endpoint'] = __( 'Endpoint', 'cac-plugin-creator' ); + $new_columns['permission'] = __( 'Permission', 'cac-plugin-creator' ); } } return $new_columns; @@ -444,27 +449,28 @@ public function add_custom_columns( $columns ) { public function custom_column_content( $column, $post_id ) { switch ( $column ) { case 'endpoint': - $endpoint = get_post_meta( $post_id, '_custom_api_endpoint', true ); + $endpoint = get_post_meta( $post_id, '_cac_plugin_endpoint', true ); if ( $endpoint ) { - echo esc_url( home_url( '/wp-json/custom-api/v1/' . ltrim( $endpoint, '/' ) ) ); + $link = esc_url( home_url( '/wp-json/cac-plugin/v1/' . ltrim( $endpoint, '/' ) ) ); + echo '' . $link . ""; } else { echo esc_html( '—' ); } break; case 'permission': - $access_type = get_post_meta( $post_id, '_custom_api_access_type', true ); + $access_type = get_post_meta( $post_id, '_cac_plugin_access_type', true ); if ( $access_type === 'public' ) { - esc_html_e( 'Public', 'custom-api-creator' ); + esc_html_e( 'Public', 'cac-plugin-creator' ); } else { - $roles = get_post_meta( $post_id, '_custom_api_roles', true ); + $roles = get_post_meta( $post_id, '_cac_plugin_roles', true ); if ( ! empty( $roles ) ) { $role_names = array_map( function ($role) { return translate_user_role( $role ); }, $roles ); echo esc_html( implode( ', ', $role_names ) ); } else { - esc_html_e( 'No roles specified', 'custom-api-creator' ); + esc_html_e( 'No roles specified', 'cac-plugin-creator' ); } } break; @@ -472,4 +478,4 @@ public function custom_column_content( $column, $post_id ) { } } -new Custom_API_Creator(); \ No newline at end of file +new CAC_Plugin_Class(); \ No newline at end of file diff --git a/readme.txt b/readme.txt index d23855a..20ca1b1 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: https://www.buymeacoffee.com/mehdiraized Tags: api, rest api, custom api, api builder, wp json Requires at least: 5.0 Tested up to: 6.6.1 -Stable tag: 1.0.2 +Stable tag: 1.0.3 Requires PHP: 7.0 License: GPLv2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html
- -

+

+ class="button">
- +
$name ) : $checked = in_array( $role, $roles ); ?> -
+