Skip to content

Commit

Permalink
Force CPTs that don't declare show_in_rest to be shown thus allowing …
Browse files Browse the repository at this point in the history
…for the export of all CPTs - see #14
  • Loading branch information
vladolaru committed Jan 12, 2022
1 parent 2a7dccb commit 778393e
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 6 deletions.
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
>=14.17.3
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The selected data is available at the endpoint `/wp-json/sce/v1/data` as a JSON

## About data

@todo Need to update this to the new `must-import` and `optional content/data` structure!

### Images

If you have images under copyright and you don't want to export them, this plugin offers you two fields :
Expand Down
5 changes: 4 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ gulp.task( 'remove-files', function() {
// files that should not be present in build zip
var files_to_remove = [
'node_modules',
'node-tasks',
'bin',
'tests',
'.travis.yml',
'.babelrc',
'.gitignore',
'.codeclimate.yml',
'.node-version',
'.npmrc',
'.nvmrc',
'.csslintrc',
'.eslintignore',
'.eslintrc',
Expand Down
31 changes: 31 additions & 0 deletions node-tasks/lock_node_version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* This script executes before "npm install"
* Lock the version of Node running based on the one set in the package.json
*/

const fs = require('fs');
const path = require('path');
const semver = require ('semver');

const packageJson = require('../package.json');
const requiredNodeVersion = packageJson.engines.node;

const runningNodeVersion = process.version;

// set .nvmrc and .node_version to have the same version
fs.writeFileSync(path.join(__dirname, '../.node-version'), requiredNodeVersion, 'UTF8');
// This way we could have a bash or zsh script that would automatically use the latest installed version of node from this major release.
// Not ideal, but since nvm doesn't support ranges... :|
fs.writeFileSync(path.join(__dirname, '../.nvmrc'), semver.minVersion(requiredNodeVersion).major.toString(), 'UTF8');

// check that the required version of Node is running
if (!semver.satisfies(runningNodeVersion, requiredNodeVersion)) {
console.error(`
The current node version ${runningNodeVersion} does not satisfy the required version ${requiredNodeVersion} .
If you have NVM and AVN installed, just exit the project folder and cd into it again (learn more about AVN: https://github.com/wbyoung/avn).
Or you may just use NVM to install and use the needed node version.
`);

// kill the process if the required node version is not the one running
process.exit(1);
}
67 changes: 67 additions & 0 deletions node-tasks/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions node-tasks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "pixelgrade-care-node-tasks",
"description": "Tasks to ensure uniform node version usage.",
"private": false,
"sideEffects": false,
"license": "GPL-2.0+",
"dependencies": {
"semver": "^7.3.5"
}
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"Pixelgrade",
"Dashboard"
],
"engines": {
"node": ">=14.17.3",
"npm": ">=6.14.13"
},
"devDependencies": {
"del": "^6.0.0",
"gulp": "^4.0.2",
Expand Down
3 changes: 2 additions & 1 deletion socket/js/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -75121,6 +75121,7 @@ var SocketPostSelect = /*#__PURE__*/function (_React$Component) {

wpPosts.fetch({
data: {
source: "socket",
per_page: 100,
filter: query
}
Expand Down Expand Up @@ -75353,7 +75354,7 @@ var SocketTaxSelect = /*#__PURE__*/function (_React$Component) {
}

var terms = [],
url = socket.wp_rest.root + 'wp/v2/' + rest_base + '?per_page=' + query.per_page;
url = socket.wp_rest.root + 'wp/v2/' + rest_base + '?per_page=' + query.per_page + '&source=socket';
fetch(url).then(function (response) {
return response.json();
}).then(function (results) {
Expand Down
1 change: 1 addition & 0 deletions socket/src/components/postSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class SocketPostSelect extends React.Component {

wpPosts.fetch({
data: {
source: "socket",
per_page: 100,
filter: query
}
Expand Down
2 changes: 1 addition & 1 deletion socket/src/components/taxSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class SocketTaxSelect extends React.Component {
}

let terms = [],
url = socket.wp_rest.root + 'wp/v2/' + rest_base + '?per_page=' + query.per_page
url = socket.wp_rest.root + 'wp/v2/' + rest_base + '?per_page=' + query.per_page + '&source=socket'

fetch(url)
.then((response) => {
Expand Down
79 changes: 76 additions & 3 deletions starter_content_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
* Plugin Name: Starter Content Exporter
* Plugin URI: https://pixelgrade.com/
* Description: A plugin which exposes exportable data through the REST API.
* Version: 1.2.2
* Version: 1.2.3
* Author: Pixelgrade, Vlad Olaru
* Author URI: https://pixelgrade.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: socket
* Domain Path: /languages
* Requires at least: 5.5.0
* Tested up to: 5.8.1
* Requires PHP: 5.6.20
* Tested up to: 5.8.3
* Requires PHP: 7.4
*/

if ( ! class_exists( 'Starter_Content_Exporter' ) ) {
Expand Down Expand Up @@ -95,6 +95,15 @@ public function __construct() {
add_action( 'init', [ $this, 'init_demo_exporter' ], 100050 );
add_filter( 'socket_config_for_starter_content_exporter', [ $this, 'add_socket_config' ] );

/**
* Add REST API support to all registered post types that don't specify a REST API behavior.
*
* This is OK since we use Starter Content Exporter on demo sites and there is nothing sensitive to expose.
*/
add_filter( 'register_post_type_args', [ $this, 'add_restapi_post_type_args' ], 10, 2 );
// Do the same for taxonomies.
add_filter( 'register_taxonomy_args', [ $this, 'add_restapi_taxonomy_args' ], 10, 2 );

// The new standard following endpoints
add_action( 'rest_api_init', [ $this, 'add_rest_routes_api_v2' ] );

Expand All @@ -105,6 +114,11 @@ public function __construct() {
// widgets
add_filter( 'pixcare_sce_widget_data_export_text', [ $this, 'prepare_text_widgets' ], 10, 1 );
add_filter( 'pixcare_sce_widget_data_export_nav_menu', [ $this, 'prepare_menu_widgets' ], 10, 1 );

// Make sure that queries don't get an unbound `posts_per_page` value (-1), via filtering.
// since the REST API core controllers (like WP_REST_Posts_Controller) don't support that.
// @see https://core.trac.wordpress.org/ticket/43998
add_action( 'rest_api_init', [ $this, 'prevent_unbound_rest_queries' ] );
}

/**
Expand All @@ -124,6 +138,65 @@ public function init_demo_exporter() {
require_once( plugin_dir_path( __FILE__ ) . 'safe-svg.php' );
}

/**
* Add the `show_in_rest` CPT argument if it is not already present.
*
* @param $args
* @param $post_type
*
* @return mixed
*/
public function add_restapi_post_type_args( $args, $post_type ) {
// We don't want to mess with these since they are system-related.
$excluded_cpts = [
'revision',
'customize_changeset',
'oembed_cache',
'user_request',
];

if ( ! in_array( $post_type, $excluded_cpts ) && is_array( $args ) && ! isset( $args['show_in_rest'] ) ) {
$args['show_in_rest'] = true;
}

return $args;
}

/**
* Add the `show_in_rest` taxonomy argument if it is not already present.
*
* @param $args
*
* @return mixed
*/
public function add_restapi_taxonomy_args( $args ) {
if ( is_array( $args ) && ! isset( $args['show_in_rest'] ) ) {
$args['show_in_rest'] = true;
}

return $args;
}

/**
* For REST API requests, prevent unbound `posts_per_page` value (-1).
*
* Nova_Restaurant CPT is filtering the posts query and imposing a -1 value,
* thus making it impossible to fetch food menu items via REST API.
*
* The REST API core controllers (like WP_REST_Posts_Controller) don't support that.
* @see https://core.trac.wordpress.org/ticket/43998
*
* @return void
*/
public function prevent_unbound_rest_queries() {
add_action( 'parse_query', function( $query ) {
if ( isset( $query->query_vars['posts_per_page'] ) && -1 === $query->query_vars['posts_per_page'] ) {
// 100 posts is the maximum allowed per page by the core REST controllers.
$query->query_vars['posts_per_page'] = 100;
}
}, 9999 );
}

/**
* This is the management interface config via the Socket options framework
*
Expand Down

0 comments on commit 778393e

Please sign in to comment.