{"id":23006340,"url":"https://github.com/bshiluk/rest-api-data-localizer","last_synced_at":"2025-08-14T02:33:09.824Z","repository":{"id":268185360,"uuid":"170609007","full_name":"bshiluk/rest-api-data-localizer","owner":"bshiluk","description":"Localize a store of normalized WP REST API response data.","archived":false,"fork":false,"pushed_at":"2020-08-05T08:13:14.000Z","size":7,"stargazers_count":40,"open_issues_count":3,"forks_count":17,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-12-15T02:03:20.784Z","etag":null,"topics":["angular","react","vue","wordpress","wordpress-plugin","wp","wp-rest-api"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bshiluk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-14T01:51:37.000Z","updated_at":"2024-08-18T17:07:33.000Z","dependencies_parsed_at":"2024-12-15T02:03:25.099Z","dependency_job_id":"e3511bc8-5a5b-41c8-91d6-2d4409018133","html_url":"https://github.com/bshiluk/rest-api-data-localizer","commit_stats":null,"previous_names":["bshiluk/rest-api-data-localizer"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bshiluk%2Frest-api-data-localizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bshiluk%2Frest-api-data-localizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bshiluk%2Frest-api-data-localizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bshiluk%2Frest-api-data-localizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bshiluk","download_url":"https://codeload.github.com/bshiluk/rest-api-data-localizer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229795874,"owners_count":18125286,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular","react","vue","wordpress","wordpress-plugin","wp","wp-rest-api"],"created_at":"2024-12-15T08:08:56.545Z","updated_at":"2024-12-15T08:08:57.079Z","avatar_url":"https://github.com/bshiluk.png","language":"PHP","readme":"# WP REST API Data Localizer\n\n\u003e Localize a store of normalized WP REST API response data. Useful for sharing data and avoiding initial API requests when using Wordpress and the WP REST API in combination with front-end frameworks like Vue, React, and Angular.\n\n## Getting Started\n\n1. Upload this repo to your `/wp-content/plugins/` directory\n2. Activate as you would any other Wordpress plugin\n\n## Usage\n\n### Initialize your store\n\n````php\nnew RADL( 'my_store', 'script_handle', array());\n````\n\n- `'my_store'` is the name of your localized store\n- `'script_handle'` is the handle of the script your store will be localized to\n- `'array()'` defines the schema for your store\n\n### Initialize your store with an endpoint\n\n````php\nnew RADL( 'my_store', 'script_handle', array(\n  'my_posts' =\u003e RADL::endpoint( 'posts' )\n));\n````\n- `'my_posts'` is the key used to call the endpoint\n- `RADL::endpoint( 'posts' )` initializes an endpoint corresponding to `'/wp/v2/posts'`\n\n#### Now you can make requests to `'/wp/v2/posts'` internally.\n\n````php\n// request the 10 most recent posts\nRADL::get('my_posts', array( 'per_page' =\u003e 10 ));\n\n// request a post with an id of 12\nRADL::get('my_posts', 12);\n\n````\n  - If the second argument is an array, it is used as the arguments for the request, otherwise it is treated as the unique identifier for a resource at the specified endpoint\n    - The first request is equivalent to `GET /wp/v2/posts/?per_page=10`\n    - The second request is equivalent to `GET /wp/v2/posts/12`\n\n#### Access WP REST API Data in Templates\n\n````php\n/**\n * Single.php\n */\n\n$my_post = RADL::get('my_posts', get_the_ID());\n\necho $my_post['title']['rendered'];\n// outputs title of post\n````\n\n### Preload endpoint in store with response data\n\n````php\nnew RADL( 'my_store', 'script_handle', array(\n  'my_posts' =\u003e RADL::endpoint( 'posts', array( 12, array( 'per_page' =\u003e 10 ) ) )\n));\n````\n- The second argument of `RADL::endpoint()` is an array of request arguments\n- Is the equivalent of calling `RADL::get()` for each value\n  - `12` adds response data from `GET /wp/v2/posts/12`\n  - `array( 'per_page' =\u003e 10 )` adds response data from `GET /wp/v2/posts/?per-page=10`\n\n### Initialize your store with a callback\n\n````php\nnew RADL( 'my_store', 'script_handle', array(\n  'site' =\u003e RADL::callback( 'site_info' )\n));\n\nfunction site_info( $arg = 'default' ) {\n  return array(\n    'name' =\u003e get_bloginfo('name'),\n    'desc' =\u003e get_bloginfo('desc'),\n    'url'  =\u003e get_bloginfo('wp_url'),\n    'arg'  =\u003e $arg\n  );\n}\n````\n- `'site'` is the key used to access data returned from the callback\n- `RADL::callback('site_info')` initializes a callback where `'site_info'` is a callable that will be called by `call_user_func_array()`\n\n#### Call and access returned data\n\n````php\necho RADL::get( 'site' )['name'];\n// outputs equivalent of get_bloginfo('name')\n\necho RADL::get( 'site', array( 'too late' ) )['arg'];\n// outputs 'default'\n// would output 'too late' if not previously called without arguments\n````\n- Callbacks are only called once \n- If not called explicitly, will be called by default with no arguments before being localized\n\n### Initializing Store With Nested Structure\n````php\nnew RADL( 'my_store', 'script_handle', array(\n  'state' =\u003e array(\n    'posts' =\u003e RADL::endpoint( 'posts' ),\n    'site'  =\u003e RADL::callback( 'site_info' )\n));\n````\n- Use dot syntax for nested keys when using `RADL::get()`\n  - `RADL::get( 'state.posts', array( 'per_page' =\u003e 10 ) )`\n  - `RADL::get( 'state.site', array( 'some_val' ) )`\n\n## Complete Example\n*As used in [Vue.wordpress](https://github.com/bucky355/vue-wordpress/)*\n````php\nnew RADL( '__VUE_WORDPRESS__', 'vue_wordpress.js', array(\n    'routing' =\u003e RADL::callback( 'vue_wordpress_routing' ),\n    'state' =\u003e array(\n        'categories' =\u003e RADL::endpoint( 'categories'),\n        'media' =\u003e RADL::endpoint( 'media', array( 7 ) ),\n        'menus' =\u003e RADL::callback( 'vue_wordpress_menus' ),\n        'pages' =\u003e RADL::endpoint( 'pages' ),\n        'posts' =\u003e RADL::endpoint( 'posts', array( array( 'per_page' =\u003e 6 ) ) ),\n        'tags'  =\u003e RADL::endpoint( 'tags' ),\n        'users' =\u003e RADL::endpoint( 'users' ),\n        'site'  =\u003e RADL::callback( 'vue_wordpress_site' ),\n    )\n) );\n````\nTo view example output:\n1. Go to [vue-wordpress.com](http://vue-wordpress.com)\n2. Open Developer Tools\n3. Type `__VUE_WORDPRESS__` in the console and press enter\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbshiluk%2Frest-api-data-localizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbshiluk%2Frest-api-data-localizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbshiluk%2Frest-api-data-localizer/lists"}