| 1 |
<?php |
| 2 |
/** |
| 3 |
* CT Hooks |
| 4 |
* |
| 5 |
* @since 1.0.0 |
| 6 |
*/ |
| 7 |
// Exit if accessed directly |
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Registers CT REST API routes. |
| 12 |
* |
| 13 |
* @since 1.0.0 |
| 14 |
*/ |
| 15 |
function ct_rest_api_init() { |
| 16 |
|
| 17 |
global $ct_registered_tables; |
| 18 |
|
| 19 |
foreach( $ct_registered_tables as $ct_table ) { |
| 20 |
|
| 21 |
// Skip tables that are not setup to being shown in rest |
| 22 |
if( ! $ct_table->show_in_rest ) { |
| 23 |
continue; |
| 24 |
} |
| 25 |
|
| 26 |
$class = ! empty( $ct_table->rest_controller_class ) ? $ct_table->rest_controller_class : 'CT_REST_Controller'; |
| 27 |
|
| 28 |
// Skip if rest controller class doesn't exists |
| 29 |
if ( ! class_exists( $class ) ) { |
| 30 |
continue; |
| 31 |
} |
| 32 |
|
| 33 |
$controller = new $class( $ct_table->name ); |
| 34 |
|
| 35 |
// Check if controller is subclass of WP_REST_Controller to check if should call to the register_routes() function |
| 36 |
if ( ! is_subclass_of( $controller, 'WP_REST_Controller' ) ) { |
| 37 |
continue; |
| 38 |
} |
| 39 |
|
| 40 |
$controller->register_routes(); |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
// Trigger CT rest API init hook |
| 45 |
do_action( 'ct_rest_api_init' ); |
| 46 |
} |
| 47 |
add_action( 'rest_api_init', 'ct_rest_api_init', 9 ); |
| 48 |
|
| 49 |
function ct_load_table_labels() { |
| 50 |
|
| 51 |
global $ct_registered_tables; |
| 52 |
|
| 53 |
foreach( $ct_registered_tables as $ct_table ) { |
| 54 |
|
| 55 |
$args = array( |
| 56 |
'singular' => ( property_exists( $ct_table, 'singular' ) ? $ct_table->singular : '' ), |
| 57 |
'plural' => ( property_exists( $ct_table, 'plural' ) ? $ct_table->plural : '' ), |
| 58 |
'labels' => ( property_exists( $ct_table, 'labels' ) ? (array) $ct_table->labels : '' ), |
| 59 |
); |
| 60 |
|
| 61 |
$args = apply_filters( "ct_{$ct_table->name}_labels", $args, $ct_table ); |
| 62 |
|
| 63 |
$ct_table->singular = $args['singular']; |
| 64 |
$ct_table->plural = $args['plural']; |
| 65 |
|
| 66 |
$labels = (array) ct_get_table_labels( $ct_table ); |
| 67 |
|
| 68 |
// Custom defined labels overrides default |
| 69 |
if( isset( $args['labels'] ) && is_array( $args['labels'] ) ) { |
| 70 |
$labels = wp_parse_args( $args['labels'], $labels ); |
| 71 |
} |
| 72 |
|
| 73 |
$ct_table->labels = (object) $labels; |
| 74 |
|
| 75 |
$ct_table->label = $ct_table->labels->name; |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
add_action( 'init', 'ct_load_table_labels', 10 ); |