*/ private const PUBLISH_SETTING_BY_ROLE = [ 'seller' => 'dokan_event_auto_publish', 'author' => 'author_can_publish_event', 'editor' => 'editor_can_publish_event', ]; /** * Constructor */ public function __construct() { parent::__construct( 'eventin-publish-event', 'Publish an Eventin event that is saved as a draft, making it live on the site. ' . 'Use this right after eventin-create-event once the admin has said yes to publishing, ' . 'or whenever the admin asks to publish, go live with, or make an event public. ' . 'Never call this on your own: ask the admin first and wait for a clear yes. ' . 'This changes only the status — no event field is modified. ' . 'Use eventin-list-events to find the event ID when you do not already have it.' ); } /** * Define tool properties * * @return array */ protected function properties(): array { return [ new ToolProperty( name: 'event_id', type: PropertyType::NUMBER, description: 'The numeric ID of the draft event to publish. ' . 'eventin-create-event returns this as event_id.', required: true ), ]; } /** * Human-readable label for the approval card header. * * @return string */ public function get_approval_label(): string { return 'Publish Eventin Event'; } /** * One-line summary of what this invocation will do. * * @return string */ public function format_approval_summary(): string { $inputs = $this->getInputs(); $event_id = (int) ( $inputs['event_id'] ?? 0 ); $title = $event_id ? get_the_title( $event_id ) : ''; return $title ? "Publish event `{$title}` (ID: {$event_id}) — it becomes live on the site" : "Publish event #{$event_id} — it becomes live on the site"; } /** * Human-readable version of tool inputs for the approval card. * * @return array */ public function get_approval_inputs(): array { $inputs = $this->getInputs(); $event_id = (int) ( $inputs['event_id'] ?? 0 ); if ( ! $event_id ) { return $inputs; } unset( $inputs['event_id'] ); $title = get_the_title( $event_id ); $inputs['event'] = ( $title ? $title . ' ' : '' ) . '(ID: ' . $event_id . ')'; $inputs['new_status'] = 'Published (live on the site)'; return $inputs; } /** * Execute the tool * * @param int $event_id ID of the event to publish. * @return array */ public function __invoke( int $event_id ): array { $check = $this->guard( 'etn_manage_event' ); if ( $check ) { return $check; } try { if ( $event_id <= 0 ) { return [ 'error' => 'event_id must be a positive integer.' ]; } $post = get_post( $event_id ); if ( ! $post || 'etn' !== $post->post_type ) { return [ 'error' => "No Eventin event found with ID {$event_id}." ]; } if ( 'publish' === $post->post_status ) { return [ 'success' => true, 'event_id' => $event_id, 'status' => 'publish', 'view_url' => get_permalink( $event_id ), 'message' => "Event `{$post->post_title}` (ID: {$event_id}) is already published.", ]; } $blocked = $this->publish_blocked_for_current_user(); if ( $blocked ) { return [ 'error' => $blocked ]; } $result = wp_update_post( [ 'ID' => $event_id, 'post_status' => 'publish', ], true ); if ( is_wp_error( $result ) ) { return [ 'error' => 'Failed to publish event: ' . $result->get_error_message() ]; } clean_post_cache( $event_id ); return [ 'success' => true, 'event_id' => $event_id, 'status' => 'publish', 'view_url' => get_permalink( $event_id ), 'edit_url' => get_edit_post_link( $event_id, 'raw' ), 'message' => "Event `{$post->post_title}` (ID: {$event_id}) is published and live on the site.", ]; } catch ( \Exception $e ) { Log::error( 'Eventin_Publish_Event_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to publish event: ' . $e->getMessage() ]; } } /** * Check whether Eventin blocks the current user from publishing events. * * @return string Empty string when publishing is allowed, otherwise the * message to show the admin. */ private function publish_blocked_for_current_user(): string { if ( current_user_can( 'manage_options' ) ) { return ''; } if ( ! class_exists( '\Eventin\Settings' ) ) { return ''; } $user_id = get_current_user_id(); $user = $user_id ? get_userdata( $user_id ) : null; $roles = $user ? (array) $user->roles : []; foreach ( self::PUBLISH_SETTING_BY_ROLE as $role => $setting ) { if ( in_array( $role, $roles, true ) && 'on' !== \Eventin\Settings::get( $setting ) ) { return 'Eventin does not let your user role publish events, so the event stays a draft. ' . 'An administrator has to publish it, or switch the matching option on in Eventin settings.'; } } return ''; } }