HEX
Server: nginx/1.27.0
System: Linux 20d173156d2c 6.8.0-88-generic #89-Ubuntu SMP PREEMPT_DYNAMIC Sat Oct 11 01:02:46 UTC 2025 x86_64
User: www-data (33)
PHP: 8.1.29
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/plugins/kadence-blocks/includes/class-lottieanimation-get-rest-api.php
<?php
/**
 * REST API Lottie Animation controller
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
/**
 * REST API Products controller class.
 */
class Kadence_LottieAnimation_get_REST_Controller extends WP_REST_Controller {

	/**
	 * Include property name.
	 */
	const PROP_END_POINT = 'id';

	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->namespace = 'kb-lottieanimation/v1';
		$this->rest_base = 'animations/(?P<id>[\a-zA-Z0-9]+)';
	}

	/**
	 * Registers the routes for the objects of the controller.
	 *
	 * @see register_rest_route()
	 */
	public function register_routes() {
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base,
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $this, 'get_animation' ),
					'permission_callback' => '__return_true',
				),
			)
		);
	}

	/**
	 * Retrieves Lottie Animation JSON from post
	 *
	 * @param WP_REST_Request $request Full details about the request.
	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
	 */
	public function get_animation( $request ) {
		$post_id = $request->get_param( self::PROP_END_POINT );
		$post_id = str_replace( '.json', '', $post_id);

		$post = get_post( $post_id );

		if( !$post || $post->post_type !== 'kadence_lottie' || $post->post_status !== 'publish' ) {
			return new WP_Error( 'not_found', __('Lottie animation not found', 'kadence-blocks'), array( 'status' => 404 ) );
		}

		if( json_decode($post->post_content) === null ) {
			return new WP_Error( 'invalid_json', __('Lottie animation file contains invalid json', 'kadence-blocks'), array( 'status' => 404 ) );
		}

		return json_decode($post->post_content);
	}

}