| 1 |
<?php |
| 2 |
|
| 3 |
// Hook the post rendering to the block |
| 4 |
if ( function_exists( 'register_block_type' ) ) : |
| 5 |
register_block_type( |
| 6 |
'ew-block/ew-page', |
| 7 |
array( |
| 8 |
'attributes' => array( |
| 9 |
'title' => array( |
| 10 |
'type' => 'string', |
| 11 |
'default' => esc_html__( 'Pages', 'essential-widgets' ), |
| 12 |
), |
| 13 |
'post_type' => array( |
| 14 |
'type' => 'string', |
| 15 |
'default' => 'page', |
| 16 |
), |
| 17 |
'depth' => array( |
| 18 |
'type' => 'number', |
| 19 |
'default' => 0, |
| 20 |
), |
| 21 |
'number' => array( |
| 22 |
'type' => 'number', |
| 23 |
'default' => 10, |
| 24 |
), |
| 25 |
'offset' => array( |
| 26 |
'type' => 'string', |
| 27 |
'default' => '', |
| 28 |
), |
| 29 |
'child_of' => array( |
| 30 |
'type' => 'string', |
| 31 |
'default' => '', |
| 32 |
), |
| 33 |
'include' => array( |
| 34 |
'type' => 'string', |
| 35 |
'default' => '', |
| 36 |
), |
| 37 |
'exclude' => array( |
| 38 |
'type' => 'string', |
| 39 |
'default' => '', |
| 40 |
), |
| 41 |
'exclude_tree' => array( |
| 42 |
'type' => 'string', |
| 43 |
'default' => '', |
| 44 |
), |
| 45 |
|
| 46 |
'meta_key' => array( |
| 47 |
'type' => 'string', |
| 48 |
'default' => '', |
| 49 |
), |
| 50 |
'meta_value' => array( |
| 51 |
'type' => 'string', |
| 52 |
'default' => '', |
| 53 |
), |
| 54 |
'authors' => array( |
| 55 |
'type' => 'string', |
| 56 |
'default' => '', |
| 57 |
), |
| 58 |
'link_before' => array( |
| 59 |
'type' => 'string', |
| 60 |
'default' => '', |
| 61 |
), |
| 62 |
'link_after' => array( |
| 63 |
'type' => 'string', |
| 64 |
'default' => '', |
| 65 |
), |
| 66 |
'show_date' => array( |
| 67 |
'type' => 'string', |
| 68 |
'default' => '', |
| 69 |
), |
| 70 |
'hierarchical' => array( |
| 71 |
'type' => 'boolean', |
| 72 |
'default' => true, |
| 73 |
), |
| 74 |
'sort_column' => array( |
| 75 |
'type' => 'string', |
| 76 |
'default' => 'post_title', |
| 77 |
), |
| 78 |
'sort_order' => array( |
| 79 |
'type' => 'string', |
| 80 |
'default' => 'ASC', |
| 81 |
), |
| 82 |
'date_format' => array( |
| 83 |
'type' => 'string', |
| 84 |
'default' => '', |
| 85 |
), |
| 86 |
'is_block' => array( |
| 87 |
'type' => 'boolean', |
| 88 |
'default' => true, |
| 89 |
), |
| 90 |
), |
| 91 |
'render_callback' => 'ew_page_render_shortcode', |
| 92 |
) |
| 93 |
); |
| 94 |
endif; |
| 95 |
|
| 96 |
if ( ! function_exists( 'ew_page_render_shortcode' ) ) : |
| 97 |
add_shortcode( 'ew-page', 'ew_page_render_shortcode' ); |
| 98 |
function ew_page_render_shortcode( $atts ) { |
| 99 |
$instance['title'] = $atts['title']; |
| 100 |
$instance['post_type'] = $atts['post_type']; |
| 101 |
$instance['depth'] = $atts['depth']; |
| 102 |
$instance['number'] = $atts['number']; |
| 103 |
$instance['offset'] = $atts['offset']; |
| 104 |
$instance['child_of'] = $atts['child_of']; |
| 105 |
$instance['include'] = $atts['include']; |
| 106 |
$instance['exclude'] = $atts['exclude']; |
| 107 |
$instance['exclude_tree'] = $atts['exclude_tree']; |
| 108 |
$instance['meta_key'] = $atts['meta_key']; |
| 109 |
$instance['meta_value'] = $atts['meta_value']; |
| 110 |
$instance['authors'] = $atts['authors']; |
| 111 |
$instance['link_before'] = $atts['link_before']; |
| 112 |
$instance['link_after'] = $atts['link_after']; |
| 113 |
$instance['show_date'] = $atts['show_date']; |
| 114 |
$instance['hierarchical'] = $atts['hierarchical']; |
| 115 |
$instance['sort_column'] = $atts['sort_column']; |
| 116 |
$instance['sort_order'] = $atts['sort_order']; |
| 117 |
$instance['date_format'] = $atts['date_format']; |
| 118 |
$instance['is_block'] = $atts['is_block']; |
| 119 |
|
| 120 |
$ew_page = new EW_Pages(); |
| 121 |
|
| 122 |
return $ew_page->shortcode( $instance ); |
| 123 |
} |
| 124 |
endif; |
| 125 |
|
| 126 |
if ( ! function_exists( 'ew_page_list' ) ) : |
| 127 |
|
| 128 |
/** |
| 129 |
* Get nav menus for select option via custom REST API! |
| 130 |
* |
| 131 |
* @return array|null Array of nav menus object with label and value pair, * or null if none. |
| 132 |
*/ |
| 133 |
function ew_page_list() { |
| 134 |
$post_types = get_post_types( |
| 135 |
array( |
| 136 |
'public' => true, |
| 137 |
'hierarchical' => true, |
| 138 |
), |
| 139 |
'objects' |
| 140 |
); |
| 141 |
|
| 142 |
$page_list = array(); |
| 143 |
|
| 144 |
foreach ( $post_types as $page ) { |
| 145 |
$object = new stdClass(); |
| 146 |
$object->label = $page->labels->singular_name; |
| 147 |
$object->value = $page->name; |
| 148 |
$page_list[] = $object; |
| 149 |
} |
| 150 |
|
| 151 |
return $page_list; |
| 152 |
} |
| 153 |
|
| 154 |
add_action( |
| 155 |
'rest_api_init', |
| 156 |
function () { |
| 157 |
register_rest_route( |
| 158 |
'ew-rest/v1', |
| 159 |
'ew-page-list', |
| 160 |
array( |
| 161 |
'methods' => 'GET', |
| 162 |
'callback' => 'ew_page_list', |
| 163 |
'permission_callback' => '__return_true', // for public use |
| 164 |
) |
| 165 |
); |
| 166 |
} |
| 167 |
); |
| 168 |
endif; |
| 169 |
|