Plugin Directory

Changeset 3736632


Ignore:
Timestamp:
10/09/2026 12:30:22 PM (2 weeks ago)
Author:
automaize
Message:

1.2.0: Add media library access (GET /staff/media, media_id on image upload)

Location:
automaize-event-calendar/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • automaize-event-calendar/trunk/automaize-event-calendar.php

    r3708972 r3736632  
    33 * Plugin Name: Automaize Event Calendar
    44 * Description: Displays upcoming events in a responsive tile grid with recurring event support and optional WooCommerce integration.
    5  * Version:     1.1.0
     5 * Version:     1.2.0
    66 * Author:      Automaize
    77 * Author URI:  https://automaize.app
    … …  
    1818}
    1919
    20 define( 'AMZEC_VERSION', '1.1.0' );
     20define( 'AMZEC_VERSION', '1.2.0' );
    2121define( 'AMZEC_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
    2222define( 'AMZEC_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
  • automaize-event-calendar/trunk/includes/class-aec-events-query.php

    r3708972 r3736632  
    120120                );
    121121        }
     122
     123        /**
     124         * Paginated listing of image attachments already in the WordPress
     125         * media library - lets the app offer "choose an existing image"
     126         * alongside "upload a new one" when setting an event's featured
     127         * image, instead of only ever re-uploading from the device.
     128         *
     129         * @param int $paged    1-based page number.
     130         * @param int $per_page Items per page.
     131         * @return array{ attachment_ids: int[], total: int, total_pages: int }
     132         */
     133        public static function get_media_list( $paged = 1, $per_page = 40 ) {
     134                $query = new WP_Query(
     135                        array(
     136                                'post_type'      => 'attachment',
     137                                'post_mime_type' => 'image',
     138                                'post_status'    => 'inherit',
     139                                'posts_per_page' => $per_page,
     140                                'paged'          => max( 1, (int) $paged ),
     141                                'orderby'        => 'date',
     142                                'order'          => 'DESC',
     143                                'fields'         => 'ids',
     144                        )
     145                );
     146
     147                return array(
     148                        'attachment_ids' => $query->posts,
     149                        'total'          => (int) $query->found_posts,
     150                        'total_pages'    => (int) $query->max_num_pages,
     151                );
     152        }
    122153}
  • automaize-event-calendar/trunk/includes/class-aec-events-write.php

    r3708972 r3736632  
    161161        }
    162162
     163        /**
     164         * Sets an event's featured image to an attachment already in the media
     165         * library, instead of uploading a new one - the counterpart to
     166         * set_featured_image() for the app's "choose existing image" picker.
     167         *
     168         * @param int $post_id      Event post ID.
     169         * @param int $attachment_id Existing media library attachment ID.
     170         * @return int|WP_Error The attachment ID on success.
     171         */
     172        public static function set_featured_image_from_media( $post_id, $attachment_id ) {
     173                $post = get_post( $post_id );
     174                if ( ! $post || 'amzec_event' !== $post->post_type ) {
     175                        return new WP_Error( 'aec_not_found', __( 'Event not found.', 'automaize-event-calendar' ), array( 'status' => 404 ) );
     176                }
     177
     178                $attachment = get_post( $attachment_id );
     179                if ( ! $attachment || 'attachment' !== $attachment->post_type || 0 !== strpos( (string) $attachment->post_mime_type, 'image/' ) ) {
     180                        return new WP_Error( 'aec_invalid', __( 'That media item could not be found.', 'automaize-event-calendar' ), array( 'status' => 404 ) );
     181                }
     182
     183                set_post_thumbnail( $post_id, $attachment_id );
     184
     185                return $attachment_id;
     186        }
     187
    163188        // -------------------------------------------------------------------------
    164189        // Shared field mapping
  • automaize-event-calendar/trunk/includes/class-aec-rest.php

    r3708972 r3736632  
    137137                        )
    138138                );
     139
     140                register_rest_route(
     141                        self::NAMESPACE_,
     142                        '/staff/media',
     143                        array(
     144                                'methods'             => WP_REST_Server::READABLE,
     145                                'callback'            => array( __CLASS__, 'list_media' ),
     146                                'permission_callback' => array( __CLASS__, 'check_api_key' ),
     147                        )
     148                );
    139149        }
    140150
    … …  
    385395         */
    386396        public static function upload_event_image( WP_REST_Request $request ) {
    387                 $files = $request->get_file_params();
    388 
    389                 if ( empty( $files['file'] ) ) {
    390                         return self::error_response( new WP_Error( 'aec_invalid', __( 'Please provide an image file.', 'automaize-event-calendar' ), array( 'status' => 400 ) ) );
    391                 }
    392 
    393                 $attach_id = AMZEC_Events_Write::set_featured_image( absint( $request['id'] ), $files['file'] );
     397                $files    = $request->get_file_params();
     398                $media_id = absint( $request->get_param( 'media_id' ) );
     399
     400                if ( ! empty( $files['file'] ) ) {
     401                        $attach_id = AMZEC_Events_Write::set_featured_image( absint( $request['id'] ), $files['file'] );
     402                } elseif ( $media_id > 0 ) {
     403                        // Picked an existing media library item instead of uploading a
     404                        // new file - see AMZEC_Events_Query::get_media_list() /
     405                        // GET /staff/media.
     406                        $attach_id = AMZEC_Events_Write::set_featured_image_from_media( absint( $request['id'] ), $media_id );
     407                } else {
     408                        return self::error_response( new WP_Error( 'aec_invalid', __( 'Please provide an image file or an existing media_id.', 'automaize-event-calendar' ), array( 'status' => 400 ) ) );
     409                }
    394410
    395411                if ( is_wp_error( $attach_id ) ) {
    … …  
    401417                                'attachment_id' => $attach_id,
    402418                                'image_url'     => wp_get_attachment_url( $attach_id ),
     419                        )
     420                );
     421        }
     422
     423        /**
     424         * GET /staff/media?page= - paginated image picker for the app's "choose
     425         * an existing image" option when setting an event's featured image.
     426         *
     427         * @param WP_REST_Request $request Request.
     428         * @return WP_REST_Response
     429         */
     430        public static function list_media( WP_REST_Request $request ) {
     431                $page   = max( 1, absint( $request->get_param( 'page' ) ) );
     432                $result = AMZEC_Events_Query::get_media_list( $page );
     433
     434                $items = array_map(
     435                        function ( $attachment_id ) {
     436                                $thumb = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
     437
     438                                return array(
     439                                        'id'            => $attachment_id,
     440                                        'url'           => wp_get_attachment_url( $attachment_id ),
     441                                        'thumbnail_url' => $thumb ? $thumb[0] : wp_get_attachment_url( $attachment_id ),
     442                                        'title'         => get_the_title( $attachment_id ),
     443                                );
     444                        },
     445                        $result['attachment_ids']
     446                );
     447
     448                return rest_ensure_response(
     449                        array(
     450                                'items'       => $items,
     451                                'total'       => $result['total'],
     452                                'total_pages' => $result['total_pages'],
    403453                        )
    404454                );
  • automaize-event-calendar/trunk/readme.txt

    r3708972 r3736632  
    55Tested up to:      7.0
    66Requires PHP:      7.4
    7 Stable tag:        1.1.0
     7Stable tag:        1.2.0
    88License:           GPL-2.0-or-later
    99License URI:       https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    7979== Changelog ==
    8080
     81= 1.2.0 =
     82* Added `GET /staff/media`: a paginated listing of existing image attachments in the WordPress media library, so the app can offer "choose an existing image" alongside "upload a new one" when setting an event's featured image. `POST /staff/events/{id}/image` now also accepts a `media_id` of an existing attachment as an alternative to a file upload.
     83
    8184= 1.1.0 =
    8285* New: REST API for the Kerstins mobile companion app (public event browsing plus an API-key-protected staff surface to create, edit, delete events and upload a featured image — including full recurrence support)
Note: See TracChangeset for help on using the changeset viewer.