diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml new file mode 100644 index 0000000..93a561e --- /dev/null +++ b/.github/workflows/php.yml @@ -0,0 +1,19 @@ +name: WordPress Plugin Build Test + +on: + push: + branches: + - main + - "releases/*" + pull_request: + branches: [main] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Get latest code + uses: actions/checkout@v4 + + - name: Run plugin check + uses: WordPress/plugin-check-action@v1.0.5 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8946dbe --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +# Packages and Dependencies +/node_mudules +/vendor + +# SVN Files +/svn + +# Plugin Zip File +thumbnail-remover.zip \ No newline at end of file diff --git a/assets/img/bmc-button.png b/assets/img/bmc-button.png new file mode 100644 index 0000000..9a4e617 Binary files /dev/null and b/assets/img/bmc-button.png differ diff --git a/assets/js/script.js b/assets/js/script.js new file mode 100644 index 0000000..4aba915 --- /dev/null +++ b/assets/js/script.js @@ -0,0 +1,77 @@ +jQuery(document).ready(function ($) { + var sectionTemplate = $("#api_sections .api-section").first().clone(); + var sectionCount = $("#api_sections .api-section").length; + + // Add new section + $("#add_section").on("click", function () { + var newSection = sectionTemplate.clone(); + sectionCount++; + + newSection.find("h4").text("Section " + sectionCount); + newSection.find("select, input").each(function () { + var name = $(this).attr("name"); + if (name) { + $(this).attr( + "name", + name.replace("[0]", "[" + (sectionCount - 1) + "]") + ); + } + }); + + // Clear the section name field + newSection.find('input[name$="[name]"]').val(sectionCount); + + newSection.attr("data-index", sectionCount - 1); + newSection.find(".remove-section").show(); + + $("#api_sections").append(newSection); + resetRemoveButtons(); + }); + + // Remove section + $("#api_sections").on("click", ".remove-section", function () { + $(this).closest(".api-section").remove(); + resetSectionIndexes(); + resetRemoveButtons(); + }); + + // Reset section indexes + function resetSectionIndexes() { + $("#api_sections .api-section").each(function (index) { + $(this) + .find("h4") + .text("Section " + (index + 1)); + $(this).attr("data-index", index); + $(this) + .find("select, input") + .each(function () { + var name = $(this).attr("name"); + if (name) { + $(this).attr("name", name.replace(/\[\d+\]/, "[" + index + "]")); + } + }); + }); + sectionCount = $("#api_sections .api-section").length; + } + + // Reset remove buttons + function resetRemoveButtons() { + var sections = $("#api_sections .api-section"); + sections.find(".remove-section").show(); + if (sections.length === 1) { + sections.first().find(".remove-section").hide(); + } + } + + // Handle access type switch + $('input[name="custom_api_access_type"]').on("change", function () { + if ($(this).val() === "private") { + $("#custom_api_roles_row").show(); + } else { + $("#custom_api_roles_row").hide(); + } + }); + + // Initialize + resetRemoveButtons(); +}); diff --git a/custom-api-creator.php b/custom-api-creator.php new file mode 100644 index 0000000..bbba716 --- /dev/null +++ b/custom-api-creator.php @@ -0,0 +1,494 @@ + _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 Custom API', 'custom-api-creator'), + 'new_item' => __('New Custom API', 'custom-api-creator'), + 'edit_item' => __('Edit Custom API', 'custom-api-creator'), + 'view_item' => __('View Custom API', 'custom-api-creator'), + 'all_items' => __('All Custom APIs', 'custom-api-creator'), + 'search_items' => __('Search Custom APIs', 'custom-api-creator'), + 'parent_item_colon' => __('Parent Custom APIs:', 'custom-api-creator'), + 'not_found' => __('No custom apis found.', 'custom-api-creator'), + 'not_found_in_trash' => __('No custom apis found in Trash.', 'custom-api-creator') + ); + + $args = array( + 'labels' => $labels, + 'public' => false, + 'publicly_queryable' => false, + 'show_ui' => true, + 'show_in_menu' => false, + 'query_var' => true, + 'rewrite' => array('slug' => 'custom-api'), + 'capability_type' => 'post', + 'has_archive' => false, + 'hierarchical' => false, + 'menu_position' => null, + 'supports' => array('title') + ); + + register_post_type('custom_api', $args); + } + + public function load_textdomain() + { + load_plugin_textdomain('custom-api-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'), + 'manage_options', + 'edit.php?post_type=custom_api', + null, + 'dashicons-rest-api', + 30 + ); + } + + public function enqueue_admin_scripts($hook) + { + if ('post.php' != $hook && 'post-new.php' != $hook) { + return; + } + + global $post; + if ('custom_api' !== $post->post_type) { + return; + } + + wp_enqueue_script('custom-api-admin', plugin_dir_url(__FILE__) . 'assets/js/script.js', array('jquery'), '1.0', true); + } + + public function add_custom_api_meta_boxes() + { + add_meta_box( + 'custom_api_details', + __('API Details', 'custom-api-creator'), + array($this, 'render_api_details_meta_box'), + 'custom_api', + 'normal', + 'high' + ); + } + + public function render_api_details_meta_box($post) + { + wp_nonce_field('custom_api_meta_box', 'custom_api_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(); + + $post_types = get_post_types(array('public' => true), 'objects'); + $all_roles = wp_roles()->get_names(); + $all_taxonomies = get_taxonomies(array('public' => true), 'objects'); + ?> +
+ | + + + | +
---|---|
+ |
+
+ $section) {
+ $this->render_section_fields($post_types, $all_taxonomies, $index, $section);
+ }
+ } else {
+ $this->render_section_fields($post_types, $all_taxonomies, 0);
+ }
+ ?>
+
+
+ |
+
+ | + + | +
+ +
++ +
+
+
+
+
+
+
+
+ name, $section['taxonomies']);
+ ?>
+
+
+