| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
|
| add_action( 'rest_api_init', function() {
|
|
|
|
|
| $core_fields = array(
|
| 'original_wpcode_id',
|
| 'kic_snippet_id',
|
| 'snippet_active',
|
| 'last_sync_time',
|
| );
|
|
|
| foreach ( $core_fields as $field_name ) {
|
| register_rest_field( 'wpcode_snippets_sync', $field_name, array(
|
| 'get_callback' => function( $object ) use ( $field_name ) {
|
| $value = get_field( $field_name, $object['id'] );
|
|
|
|
|
| if ( in_array( $field_name, array( 'snippet_active' ) ) ) {
|
| return (bool) $value;
|
| }
|
|
|
|
|
| return strval( $value );
|
| },
|
| 'schema' => array(
|
| 'description' => sprintf( 'Field: %s', $field_name ),
|
| 'type' => in_array( $field_name, array( 'snippet_active' ) ) ? 'boolean' : 'string',
|
| 'context' => array( 'view', 'edit' ),
|
| ),
|
| ) );
|
| }
|
|
|
|
|
| $code_fields = array(
|
| 'snippet_title',
|
| 'snippet_code',
|
| 'snippet_type',
|
| 'snippet_location',
|
| 'snippet_priority',
|
| );
|
|
|
| foreach ( $code_fields as $field_name ) {
|
| register_rest_field( 'wpcode_snippets_sync', $field_name, array(
|
| 'get_callback' => function( $object ) use ( $field_name ) {
|
| $value = get_field( $field_name, $object['id'] );
|
|
|
| if ( 'snippet_priority' === $field_name ) {
|
| return (int) $value;
|
| }
|
|
|
| return $value;
|
| },
|
| 'schema' => array(
|
| 'description' => sprintf( 'Field: %s', $field_name ),
|
| 'type' => 'snippet_priority' === $field_name ? 'integer' : 'string',
|
| 'context' => array( 'view', 'edit' ),
|
| ),
|
| ) );
|
| }
|
|
|
|
|
|
|
|
|
| register_rest_field( 'wpcode_snippets_sync', 'snippet_code_file', array(
|
| 'get_callback' => function( $object ) {
|
| $file = get_field( 'snippet_code_file', $object['id'] );
|
|
|
|
|
| if ( $file && is_array( $file ) ) {
|
| return array(
|
| 'id' => $file['ID'],
|
| 'url' => $file['url'],
|
| 'filename' => $file['filename'],
|
| 'filesize' => $file['filesize'],
|
| 'type' => $file['type'],
|
| 'subtype' => $file['subtype'],
|
| 'title' => $file['title'],
|
| );
|
| }
|
|
|
| return null;
|
| },
|
| 'schema' => array(
|
| 'description' => 'Downloadable code file attachment',
|
| 'type' => 'object',
|
| 'context' => array( 'view', 'edit' ),
|
| 'properties' => array(
|
| 'id' => array( 'type' => 'integer' ),
|
| 'url' => array( 'type' => 'string', 'format' => 'uri' ),
|
| 'filename' => array( 'type' => 'string' ),
|
| 'filesize' => array( 'type' => 'integer' ),
|
| 'type' => array( 'type' => 'string' ),
|
| 'subtype' => array( 'type' => 'string' ),
|
| 'title' => array( 'type' => 'string' ),
|
| ),
|
| ),
|
| ) );
|
|
|
|
|
| $targeting_fields = array(
|
| 'snippet_auto_insert',
|
| 'snippet_device_type',
|
| 'snippet_schedule_start',
|
| 'snippet_schedule_end',
|
| );
|
|
|
| foreach ( $targeting_fields as $field_name ) {
|
| register_rest_field( 'wpcode_snippets_sync', $field_name, array(
|
| 'get_callback' => function( $object ) use ( $field_name ) {
|
| $value = get_field( $field_name, $object['id'] );
|
|
|
| if ( 'snippet_auto_insert' === $field_name ) {
|
| return (bool) $value;
|
| }
|
|
|
| return $value;
|
| },
|
| 'schema' => array(
|
| 'description' => sprintf( 'Field: %s', $field_name ),
|
| 'type' => 'snippet_auto_insert' === $field_name ? 'boolean' : 'string',
|
| 'context' => array( 'view', 'edit' ),
|
| ),
|
| ) );
|
| }
|
|
|
|
|
| $metadata_fields = array(
|
| 'snippet_tags',
|
| 'snippet_description',
|
| 'snippet_dependencies',
|
| 'snippet_created_date',
|
| 'snippet_modified_date',
|
| 'snippet_author',
|
| 'snippet_version',
|
| );
|
|
|
| foreach ( $metadata_fields as $field_name ) {
|
| register_rest_field( 'wpcode_snippets_sync', $field_name, array(
|
| 'get_callback' => function( $object ) use ( $field_name ) {
|
| return get_field( $field_name, $object['id'] );
|
| },
|
| 'schema' => array(
|
| 'description' => sprintf( 'Field: %s', $field_name ),
|
| 'type' => 'string',
|
| 'context' => array( 'view', 'edit' ),
|
| ),
|
| ) );
|
| }
|
|
|
|
|
| register_rest_field( 'wpcode_snippets_sync', 'snippet_stats', array(
|
| 'get_callback' => function( $object ) {
|
| $code = get_field( 'snippet_code', $object['id'] );
|
| $schedule_start = get_field( 'snippet_schedule_start', $object['id'] );
|
| $code_file = get_field( 'snippet_code_file', $object['id'] );
|
|
|
| return array(
|
| 'code_length' => strlen( $code ),
|
| 'is_scheduled' => ! empty( $schedule_start ),
|
| 'last_synced' => get_field( 'last_sync_time', $object['id'] ),
|
| 'has_code_file' => ! empty( $code_file ),
|
| );
|
| },
|
| 'schema' => array(
|
| 'description' => 'Computed snippet statistics',
|
| 'type' => 'object',
|
| 'context' => array( 'view', 'edit' ),
|
| ),
|
| ) );
|
|
|
| } );
|
|
|
|
|
|
|
|
|
| add_filter( 'rest_wpcode_snippets_sync_query', function( $args, $request ) {
|
|
|
|
|
| if ( ! empty( $request['snippet_type'] ) ) {
|
| $args['meta_query'] = isset( $args['meta_query'] ) ? $args['meta_query'] : array();
|
| $args['meta_query'][] = array(
|
| 'key' => 'snippet_type',
|
| 'value' => sanitize_text_field( $request['snippet_type'] ),
|
| );
|
| }
|
|
|
|
|
| if ( isset( $request['active'] ) ) {
|
| if ( ! isset( $args['meta_query'] ) ) {
|
| $args['meta_query'] = array();
|
| }
|
| $args['meta_query'][] = array(
|
| 'key' => 'snippet_active',
|
| 'value' => $request['active'] ? '1' : '0',
|
| );
|
| }
|
|
|
|
|
| if ( ! empty( $request['device_type'] ) ) {
|
| if ( ! isset( $args['meta_query'] ) ) {
|
| $args['meta_query'] = array();
|
| }
|
| $args['meta_query'][] = array(
|
| 'key' => 'snippet_device_type',
|
| 'value' => sanitize_text_field( $request['device_type'] ),
|
| );
|
| }
|
|
|
| return $args;
|
| }, 10, 2 );
|
|
|
|
|
|
|
|
|
| add_filter( 'rest_wpcode_snippets_sync_collection_params', function( $params ) {
|
|
|
| $params['snippet_type'] = array(
|
| 'description' => 'Filter snippets by type (php, html, css, js, text)',
|
| 'type' => 'string',
|
| 'enum' => array( 'php', 'html', 'css', 'js', 'text' ),
|
| );
|
|
|
| $params['active'] = array(
|
| 'description' => 'Filter snippets by active status',
|
| 'type' => 'boolean',
|
| );
|
|
|
| $params['device_type'] = array(
|
| 'description' => 'Filter snippets by device type',
|
| 'type' => 'string',
|
| 'enum' => array( 'all', 'mobile', 'desktop' ),
|
| );
|
|
|
| return $params;
|
| } );
|
|
|
|
|
|
|
|
|
| add_filter( 'rest_prepare_wpcode_snippets_sync', function( $response, $post, $request ) {
|
| $data = $response->get_data();
|
|
|
|
|
| $data['_source_info'] = array(
|
| 'cpt' => 'wpcode_snippets_sync',
|
| 'original_cpt' => 'wpcode',
|
| 'sync_type' => 'one-way-mirror',
|
| 'read_only' => true,
|
| );
|
|
|
| $response->set_data( $data );
|
| return $response;
|
| }, 10, 3 );
|
|
|
|
|
|
|
|
|
| $meta_fields_to_register = array(
|
| 'original_wpcode_id' => 'string',
|
| 'kic_snippet_id' => 'string',
|
| 'snippet_active' => 'boolean',
|
| 'last_sync_time' => 'string',
|
| 'snippet_title' => 'string',
|
| 'snippet_code' => 'string',
|
| 'snippet_code_file' => 'integer',
|
| 'snippet_type' => 'string',
|
| 'snippet_location' => 'string',
|
| 'snippet_priority' => 'integer',
|
| 'snippet_auto_insert' => 'boolean',
|
| 'snippet_device_type' => 'string',
|
| 'snippet_schedule_start' => 'string',
|
| 'snippet_schedule_end' => 'string',
|
| 'snippet_tags' => 'string',
|
| 'snippet_description' => 'string',
|
| 'snippet_dependencies' => 'string',
|
| 'snippet_created_date' => 'string',
|
| 'snippet_modified_date' => 'string',
|
| 'snippet_author' => 'string',
|
| 'snippet_version' => 'string',
|
| );
|
|
|
| foreach ( $meta_fields_to_register as $meta_key => $type ) {
|
| register_post_meta( 'wpcode_snippets_sync', $meta_key, array(
|
| 'show_in_rest' => true,
|
| 'single' => true,
|
| 'type' => $type,
|
| ) );
|
| }
|
| |
| |
Comments