) { $data[ $key ] = array( 'value' => $entry->is( Lazy_Entry::class ) ? null : $entry->get(), 'nonce' => $this->registry->get_endpoint( $key )->create_nonce(), ); if ( DS_Utils::is_debug() ) { $data[ $key ]['log'] = $entry->get_parser()->get_log(); } if ( DS_Utils::debug_disable( $key ) ) { unset( $data[ $key ]['value'] ); } if ( $entry->is( Lazy_Entry::class ) ) { $data[ $key ]['lazy'] = true; } // Include nonces for action endpoints associated with this entry $action_nonces = $this->get_action_nonces_for_entry( $key ); if ( ! empty( $action_nonces ) ) { $data[ $key ]['actions'] = $action_nonces; } } wp_localize_script( $this->script_handle, $this->namespace, $data ); } public function attach_to_plugin( $script_handle, $plugin_page_hook ) { $this->script_handle = $script_handle; add_action( $plugin_page_hook, array( $this, '_print_options_script_tag' ) ); } public function get_registry() { return $this->registry; } /** * DataSync entries have to be registered before they can be used. * * Typically, entries are stored in WP Options, so this method * is will default to registering entries as Data_Sync_Option. * * However, you can provide an `$entry` instance that subscribes Entry_Can_* methods. * If you do, `Entry_Can_Get` interface is required, and all other Entry_Can_* interfaces are optional. * * @param string $key - The key to register the entry under. * @param Parser $parser - The parser to use for the entry. * @param Entry_Can_Get $custom_entry_instance - The entry to register. If null, a new Data_Sync_Option will be created. * * @return void */ public function register( $key, $parser, $custom_entry_instance = null ) { $option_key = $this->namespace . '_' . $key; // If a custom entry instance is provided, and it implements Entry_Can_Get, use that. // Otherwise, this Entry will store data using Data_Sync_Option (wp_options). $entry = ( $custom_entry_instance instanceof Entry_Can_Get ) ? $custom_entry_instance : new Data_Sync_Option( $option_key ); /* * ## Adapter * This `register` method is inteded to be a shorthand for the most common use case. * * Custom entries can implement various interfaces depending on whether they can set, merge, delete, etc. * However, the Registry expects an object that implements Data_Sync_Entry. * That's why we wrap the Entry in an Adapter - giving it a guaranteed interface. * * ## Customization * Entries can be flexible because they're wrapped in an Adapter. * But you can also create a class that implements `Data_Sync_Entry` directly if you need to. * In that case, you'd need to use: * ```php * $Data_Sync->get_registry()->register(...)` instead of `$Data_Sync->register(...) * ``` */ if ( method_exists( $parser, 'set_context' ) ) { // @phan-suppress-next-line PhanUndeclaredMethod -- Phan misses the method_exists(). See https://github.com/phan/phan/issues/1204. $parser->set_context( new Schema_Context( $key ) ); } $entry_adapter = new Data_Sync_Entry_Adapter( $entry, $parser ); $this->registry->register( $key, $entry_adapter ); } public function register_action( $key, $action_name, $request_schema, $instance ) { $this->registry->register_action( $key, $action_name, $request_schema, $instance ); } }