| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
|
| add_action( 'rest_api_init', function() {
|
|
|
|
|
|
|
|
|
| $source_fields = array(
|
| 'source_type',
|
| 'file_path',
|
| );
|
|
|
| foreach ( $source_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'] );
|
|
|
|
|
| return $value ? sanitize_text_field( strval( $value ) ) : '';
|
| },
|
| 'update_callback' => null,
|
| 'schema' => array(
|
| 'description' => sprintf( 'Field: %s', $field_name ),
|
| 'type' => 'string',
|
| 'context' => array( 'view', 'edit' ),
|
| 'readonly' => true,
|
| ),
|
| ) );
|
| }
|
|
|
|
|
| $core_fields = array(
|
| 'original_wpcode_id',
|
| 'kic_snippet_id',
|
| 'snippet_active',
|
| 'last_sync_time',
|
| );
|
|
|
|
|
| $boolean_fields = array( 'snippet_active' );
|
|
|
| foreach ( $core_fields as $field_name ) {
|
| register_rest_field( 'wpcode_snippets_sync', $field_name, array(
|
| 'get_callback' => function( $object ) use ( $field_name, $boolean_fields ) {
|
| $value = get_field( $field_name, $object['id'] );
|
|
|
|
|
| if ( in_array( $field_name, $boolean_fields, true ) ) {
|
| return (bool) $value;
|
| }
|
|
|
|
|
| return strval( $value );
|
| },
|
| 'update_callback' => null,
|
| 'schema' => array(
|
| 'description' => sprintf( 'Field: %s', $field_name ),
|
| 'type' => in_array( $field_name, $boolean_fields, true ) ? 'boolean' : 'string',
|
| 'context' => array( 'view', 'edit' ),
|
| 'readonly' => true,
|
| ),
|
| ) );
|
| }
|
|
|
|
|
| $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 absint( $value );
|
| }
|
|
|
| return $value;
|
| },
|
| 'update_callback' => null,
|
| 'schema' => array(
|
| 'description' => sprintf( 'Field: %s', $field_name ),
|
| 'type' => 'snippet_priority' === $field_name ? 'integer' : 'string',
|
| 'context' => array( 'view', 'edit' ),
|
| 'readonly' => true,
|
| ),
|
| ) );
|
| }
|
|
|
|
|
|
|
|
|
| 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 ) && ! empty( $file['url'] ) ) {
|
|
|
| return esc_url( add_query_arg( 'download', '1', $file['url'] ) );
|
| }
|
|
|
|
|
| if ( is_string( $file ) && filter_var( $file, FILTER_VALIDATE_URL ) ) {
|
| return esc_url( add_query_arg( 'download', '1', $file ) );
|
| }
|
|
|
|
|
| return '';
|
| },
|
| 'update_callback' => null,
|
| 'schema' => array(
|
| 'description' => 'Direct download URL to code file (with force download parameter)',
|
| 'type' => 'string',
|
| 'format' => 'uri',
|
| 'context' => array( 'view', 'edit' ),
|
| 'readonly' => true,
|
| ),
|
| ) );
|
|
|
|
|
| $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;
|
| },
|
| 'update_callback' => null,
|
| 'schema' => array(
|
| 'description' => sprintf( 'Field: %s', $field_name ),
|
| 'type' => 'snippet_auto_insert' === $field_name ? 'boolean' : 'string',
|
| 'context' => array( 'view', 'edit' ),
|
| 'readonly' => true,
|
| ),
|
| ) );
|
| }
|
|
|
|
|
| $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'] );
|
| },
|
| 'update_callback' => null,
|
| 'schema' => array(
|
| 'description' => sprintf( 'Field: %s', $field_name ),
|
| 'type' => 'string',
|
| 'context' => array( 'view', 'edit' ),
|
| 'readonly' => true,
|
| ),
|
| ) );
|
| }
|
|
|
|
|
| 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'] );
|
| $source_type = get_field( 'source_type', $object['id'] );
|
|
|
| return array(
|
| 'code_length' => is_string( $code ) ? strlen( $code ) : 0,
|
| 'is_scheduled' => ! empty( $schedule_start ),
|
| 'last_synced' => get_field( 'last_sync_time', $object['id'] ),
|
| 'has_code_file' => ! empty( $code_file ),
|
| 'source_type' => $source_type ? sanitize_key( $source_type ) : 'wpcode',
|
| 'is_mu_plugin' => 'mu-plugin' === $source_type,
|
| );
|
| },
|
| 'update_callback' => null,
|
| 'schema' => array(
|
| 'description' => 'Computed snippet statistics',
|
| 'type' => 'object',
|
| 'context' => array( 'view', 'edit' ),
|
| 'readonly' => true,
|
| 'properties' => array(
|
| 'code_length' => array( 'type' => 'integer' ),
|
| 'is_scheduled' => array( 'type' => 'boolean' ),
|
| 'last_synced' => array( 'type' => 'string' ),
|
| 'has_code_file' => array( 'type' => 'boolean' ),
|
| 'source_type' => array( 'type' => 'string' ),
|
| 'is_mu_plugin' => array( 'type' => 'boolean' ),
|
| ),
|
| ),
|
| ) );
|
|
|
| } );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| add_filter( 'rest_wpcode_snippets_sync_query', function( $args, $request ) {
|
|
|
|
|
| if ( ! isset( $args['meta_query'] ) ) {
|
| $args['meta_query'] = array();
|
| }
|
|
|
|
|
| if ( ! empty( $request['snippet_type'] ) ) {
|
| $args['meta_query'][] = array(
|
| 'key' => 'snippet_type',
|
| 'value' => sanitize_text_field( $request['snippet_type'] ),
|
| );
|
| }
|
|
|
|
|
| if ( isset( $request['active'] ) ) {
|
| $args['meta_query'][] = array(
|
| 'key' => 'snippet_active',
|
| 'value' => rest_sanitize_boolean( $request['active'] ) ? '1' : '0',
|
| );
|
| }
|
|
|
|
|
| if ( ! empty( $request['device_type'] ) ) {
|
| $args['meta_query'][] = array(
|
| 'key' => 'snippet_device_type',
|
| 'value' => sanitize_text_field( $request['device_type'] ),
|
| );
|
| }
|
|
|
|
|
| if ( ! empty( $request['source_type'] ) ) {
|
| $args['meta_query'][] = array(
|
| 'key' => 'source_type',
|
| 'value' => sanitize_key( $request['source_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' ),
|
| 'sanitize_callback' => 'sanitize_text_field',
|
| 'validate_callback' => 'rest_validate_request_arg',
|
| );
|
|
|
| $params['active'] = array(
|
| 'description' => 'Filter snippets by active status',
|
| 'type' => 'boolean',
|
| 'sanitize_callback' => 'rest_sanitize_boolean',
|
| 'validate_callback' => 'rest_validate_request_arg',
|
| );
|
|
|
| $params['device_type'] = array(
|
| 'description' => 'Filter snippets by device type',
|
| 'type' => 'string',
|
| 'enum' => array( 'all', 'mobile', 'desktop' ),
|
| 'sanitize_callback' => 'sanitize_text_field',
|
| 'validate_callback' => 'rest_validate_request_arg',
|
| );
|
|
|
| $params['source_type'] = array(
|
| 'description' => 'Filter by source type (wpcode or mu-plugin)',
|
| 'type' => 'string',
|
| 'enum' => array( 'wpcode', 'mu-plugin' ),
|
| 'sanitize_callback' => 'sanitize_key',
|
| 'validate_callback' => 'rest_validate_request_arg',
|
| );
|
|
|
| return $params;
|
| } );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| add_filter( 'rest_prepare_wpcode_snippets_sync', function( $response, $post, $request ) {
|
| $data = $response->get_data();
|
|
|
|
|
| $source_type = get_field( 'source_type', $post->ID );
|
|
|
|
|
| $data['_source_info'] = array(
|
| 'cpt' => 'wpcode_snippets_sync',
|
| 'original_cpt' => 'mu-plugin' === $source_type ? 'mu-plugin-file' : 'wpcode',
|
| 'source_type' => $source_type ? sanitize_key( $source_type ) : 'wpcode',
|
| 'sync_type' => 'one-way-mirror',
|
| 'read_only' => true,
|
| );
|
|
|
| $response->set_data( $data );
|
| return $response;
|
| }, 10, 3 );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| $meta_fields_to_register = array(
|
|
|
| 'source_type' => 'string',
|
| 'file_path' => 'string',
|
|
|
| 'original_wpcode_id' => 'string',
|
| 'kic_snippet_id' => 'string',
|
| 'snippet_active' => 'boolean',
|
| 'last_sync_time' => 'string',
|
| 'snippet_title' => 'string',
|
| 'snippet_code' => 'string',
|
| 'snippet_code_file' => 'string',
|
| '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,
|
| 'sanitize_callback' => 'string' === $type ? 'sanitize_text_field' : null,
|
| ) );
|
| }
|
| |
| |
Comments