Skip to content

Instantly share code, notes, and snippets.

@permpkin
Last active May 8, 2022 07:32
Show Gist options
  • Save permpkin/53cf60dd16491e9c1795b5ca08d5ce3e to your computer and use it in GitHub Desktop.
Save permpkin/53cf60dd16491e9c1795b5ca08d5ce3e to your computer and use it in GitHub Desktop.
Rest Routes + LiveReload for WP Factory
<?php
/*
Plugin Name: WP Factory Utils
Plugin URI: https://gist.github.com/permpkin/53cf60dd16491e9c1795b5ca08d5ce3e
Description: Utils for use on local with wp-factory.
Version: 1.0.0
Author: Permpkin
Author URI: https://github.com/permpkin/
*/
// workaround for block script issue (Pre WP6+)
global $wp_version;
if ((int)$wp_version < 6) {
add_filter('plugins_url',function($url,$path,$plugin) {
if (strpos($url, get_template_directory()) !== false) {
$url = get_template_directory_uri().'/blocks/'.basename(dirname($plugin)).'/'.$path;
}
return $url;
},10,3);
}
// register clockwork endpoint
add_action('rest_api_init', function(){
if ( empty( $_SERVER['REQUEST_URI'] ) ) {
// Probably a CLI request
return false;
}
$json_prefix = '/wp-json/';
register_rest_route('debug', '/factory/(?P<method>[a-zA-Z0-9-_]+)', array(
'methods' => 'GET',
'callback' => function($data){
switch($data['method']) {
case 'post_types':
echo json_encode(get_post_types());
break;
case 'location_params':
echo json_encode(acf_get_location_rule_types());
break;
}
exit;
},
'permission_callback' => '__return_true'
));
// post routes
register_rest_route('debug', '/factory/(?P<method>[a-zA-Z0-9-_]+)', array(
'methods' => 'POST',
'callback' => function($data){
$json = json_decode(file_get_contents('php://input'), true);
switch($data['method']) {
case 'location_operator':
echo json_encode(acf_get_location_rule_operators( [ 'param' => $json['rule']] ));
break;
case 'location_values':
echo json_encode(acf_get_location_rule_values( [ 'param' => $json['rule']] ));
break;
}
exit;
},
'permission_callback' => '__return_true'
));
});
// add shortcut to wp-admin
add_action( 'admin_bar_menu', function(WP_Admin_Bar $admin_bar){
$admin_bar->add_menu([
'id' => 'factory',
'title' => 'Wordpress Factory',
'parent' => 'wp-logo',
'href' => '/wp/factory',
'group' => false
]);
}, 500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment