| @@ -105,8 +105,26 @@ | ||
| 105 | 105 | ); |
| 106 | 106 | |
| 107 | 107 | register_rest_route( |
| 108 | 108 | self::PREFIX, |
| 109 | + 'link-preview', | |
| 110 | + array( | |
| 111 | + 'methods' => 'POST', | |
| 112 | + 'callback' => array( $this, 'rest_link_preview' ), | |
| 113 | + 'permission_callback' => function () { | |
| 114 | + return current_user_can( Friends::REQUIRED_ROLE ); | |
| 115 | + }, | |
| 116 | + 'args' => array( | |
| 117 | + 'id' => array( | |
| 118 | + 'type' => 'integer', | |
| 119 | + 'required' => true, | |
| 120 | + ), | |
| 121 | + ), | |
| 122 | + ) | |
| 123 | + ); | |
| 124 | + | |
| 125 | + register_rest_route( | |
| 126 | + self::PREFIX, | |
| 109 | 127 | 'extension', |
| 110 | 128 | array( |
| 111 | 129 | 'methods' => array( 'GET', 'POST' ), |
| 112 | 130 | 'callback' => array( $this, 'rest_extension' ), |
| @@ -290,8 +308,29 @@ | ||
| 290 | 308 | 'was_polled' => $was_polled, |
| 291 | 309 | ); |
| 292 | 310 | } |
| 293 | 311 | |
| 312 | + /** | |
| 313 | + * Fetch a link preview on demand. | |
| 314 | + * | |
| 315 | + * @param \WP_REST_Request $request The REST request. | |
| 316 | + * @return array|\WP_Error | |
| 317 | + */ | |
| 318 | + public function rest_link_preview( $request ) { | |
| 319 | + $post_id = intval( $request->get_param( 'id' ) ); | |
| 320 | + $post = get_post( $post_id ); | |
| 321 | + | |
| 322 | + if ( ! $post || ! in_array( $post->post_type, apply_filters( 'friends_frontend_post_types', array() ), true ) ) { | |
| 323 | + return new \WP_Error( 'friends_link_preview_invalid_post', __( 'The requested post is not available.', 'friends' ), array( 'status' => 404 ) ); | |
| 324 | + } | |
| 325 | + | |
| 326 | + $preview = Link_Preview::update_link_preview( $post_id ); | |
| 327 | + | |
| 328 | + return array( | |
| 329 | + 'preview' => $preview ? $preview : false, | |
| 330 | + ); | |
| 331 | + } | |
| 332 | + | |
| 294 | 333 | public function rest_extension( $request ) { |
| 295 | 334 | $return = array( |
| 296 | 335 | 'version' => Friends::VERSION, |
| 297 | 336 | 'friends_url' => home_url( '/friends/' ), |
| @@ -306,8 +345,9 @@ | ||
| 306 | 345 | /** |
| 307 | 346 | * Allows plugins to register actions for the Friends browser extension. |
| 308 | 347 | * |
| 309 | 348 | * Each action is an associative array with: |
| 349 | + * - `id` (string, optional) — stable action identifier for clients that persist action state. | |
| 310 | 350 | * - `name` (string, required) — label shown in the extension popup. |
| 311 | 351 | * - `url` (string, required) — target URL; may contain `{current_url}` which the extension substitutes with the current page URL (URL-encoded). |
| 312 | 352 | * - `method` (string, optional) — if `"POST"`, the extension submits a form instead of opening a link. |
| 313 | 353 | * - `fields` (object, optional) — for POST actions, key/value pairs of form fields; values may contain `{current_url}` (raw) and `{page_html}` placeholders. |
| @@ -312,9 +352,14 @@ | ||
| 312 | 352 | * - `method` (string, optional) — if `"POST"`, the extension submits a form instead of opening a link. |
| 313 | 353 | * - `fields` (object, optional) — for POST actions, key/value pairs of form fields; values may contain `{current_url}` (raw) and `{page_html}` placeholders. |
| 314 | 354 | * - `run` (string, optional) — if `"inline"`, the extension handles the response in place instead of opening a new tab. |
| 315 | 355 | * - `inputs` (array, optional) — user-editable fields for inline actions. |
| 356 | + * - `submit_label` (string, optional) — label for the inline action submit button. | |
| 316 | 357 | * - `category` (string, optional) — groups actions under a named header; actions without a category appear under the default "Actions" header. |
| 358 | + * | |
| 359 | + * Inline action responses may include `message`, `edit_url`, and `link_label`. They may also | |
| 360 | + * include `fields`, `values`, and `submit_label` to let the browser extension update the same | |
| 361 | + * inline form for follow-up edits after the first action has created server-side state. | |
| 317 | 362 | * |
| 318 | 363 | * Example: |
| 319 | 364 | * ```php |
| 320 | 365 | * add_filter( 'friends_browser_extension_actions', function ( $actions, $current_user, $context ) { |