forked from bitterendio/wyvern
/
functions.php
executable file
·255 lines (203 loc) · 7.4 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/*
|--------------------------------------------------------------------------
| Theme support
|--------------------------------------------------------------------------
|
| title-tag: allows theme to put content in <title> tag
| menus: allows theme to have menus
|
*/
add_theme_support( 'title-tag' );
add_theme_support( 'menus' );
add_theme_support( 'post-thumbnails' );
/*
|--------------------------------------------------------------------------
| Styles and scripts
|--------------------------------------------------------------------------
|
| Register default styles and scripts here
|
*/
function rest_theme_scripts() {
// Styles
wp_enqueue_style( 'normalize', get_template_directory_uri() . '/assets/normalize.css', false, '3.0.3' );
wp_enqueue_style( 'style', get_stylesheet_directory_uri() . '/dist/styles.css', array( 'normalize' ) );
// Pace
wp_enqueue_script( 'pace', get_template_directory_uri() . '/assets/js/pace.min.js', array(), '1.0.0', true );
// 3rd party scripts from default setup that will not be used
wp_deregister_script('mailpoet_vendor');
wp_deregister_script('mailpoet_public');
wp_deregister_style('mailpoet_public');
wp_enqueue_script( 'wyvern-vue', get_stylesheet_directory_uri() . '/dist/build.js', array(), '1.0.5', true );
$base_url = esc_url_raw( home_url() );
$base_path = rtrim( parse_url( $base_url, PHP_URL_PATH ), '/' );
wp_localize_script( 'wyvern-vue', 'wp', apply_filters( 'wyvern_wp_settings', [
'root' => esc_url_raw( rest_url() ),
'base_url' => $base_url,
'base_path' => $base_path ? $base_path . '/' : '/',
'nonce' => wp_create_nonce( 'wp_rest' ),
'site_name' => get_bloginfo( 'name' ),
'site_desc' => get_bloginfo('description'),
'routes' => rest_theme_routes(),
'assets_path' => get_stylesheet_directory_uri() . '/assets',
// Inline configurations
'show_on_front' => get_option('show_on_front'), // (posts|page) Settings -> Reading -> Front page displays
'page_on_front' => get_option('page_on_front'), // (int) Settings -> Reading -> Front page displays when "page" is selected and type is "Front page"
'page_for_posts'=> get_option('page_for_posts'), // (int) Settings -> Reading -> Front page displays when "page" is selected and type is "Posts page"
] ) );
}
add_action( 'wp_enqueue_scripts', 'rest_theme_scripts' );
/*
|--------------------------------------------------------------------------
| Routes
|--------------------------------------------------------------------------
|
| Register theme routes here
|
*/
function rest_theme_routes() {
$routes = array();
$query = new WP_Query( array(
'post_type' => 'any',
'post_status' => 'publish',
'posts_per_page' => -1,
) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$routes[] = array(
'id' => get_the_ID(),
'type' => get_post_type(),
'slug' => basename( get_permalink() ),
'link' => str_replace( site_url(), '', get_permalink() ),
'template' => get_page_template_slug()
);
}
}
wp_reset_postdata();
return $routes;
}
// Hide admin bar until links in Edit page are eventually resolved
show_admin_bar(false);
/*
|--------------------------------------------------------------------------
| ACF
|--------------------------------------------------------------------------
|
| All ACF fields to rest api
|
*/
if ( function_exists('register_api_field') ) {
add_action( 'rest_api_init', 'slug_register_acf' );
}
function slug_register_acf() {
$post_types = get_post_types(['public' => true], 'names');
foreach ($post_types as $type) {
register_api_field( $type,
'acf',
array(
'get_callback' => 'slug_get_acf',
'update_callback' => null,
'schema' => null,
)
);
}
}
function slug_get_acf( $object, $field_name, $request ) {
if ( function_exists('get_fields') )
{
$fields = get_fields($object[ 'id' ]);
return $fields !== false ? $fields : [];
} else
{
return [];
}
}
/* If CPT has archive, register archive route */
$post_types = get_post_types();
foreach( $post_types as $post_type )
{
$cpt = get_post_type_object($post_type);
if ( $cpt->has_archive )
{
$routes[] = [
'type' => 'archive',
'slug' => $cpt->name,
'link' => get_post_type_archive_link($cpt->name),
'template' => 'archive-' . $cpt->name,
];
}
}
/**
* Add REST API support to an already registered post type.
*/
add_action( 'init', 'my_custom_post_type_rest_support', 25 );
function my_custom_post_type_rest_support() {
global $wp_post_types;
$post_type_names = [
'authors',
'partner'
];
foreach( $post_type_names as $post_type_name )
{
if ( isset($wp_post_types[ $post_type_name ]) )
{
$wp_post_types[ $post_type_name ]->show_in_rest = true;
$wp_post_types[ $post_type_name ]->rest_base = $post_type_name;
$wp_post_types[ $post_type_name ]->rest_controller_class = 'WP_REST_Posts_Controller';
}
}
}
if ( !function_exists('autoload_folder') )
{
function autoload_folder($path)
{
if ($handle = opendir($path)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !is_dir($path . '/' . $entry) ) {
require_once ($path . '/' . $entry);
}
}
closedir($handle);
}
}
}
/*
|--------------------------------------------------------------------------
| API
|--------------------------------------------------------------------------
|
| Load all calls registered in separate files in /api folder
|
*/
$path = get_template_directory() . '/api';
autoload_folder($path);
/*
* Automatically install necessary plugins via TGM plugin activation
*/
require_once get_template_directory() . '/lib/TGM/class-tgm-plugin-activation.php';
require_once get_template_directory() . '/lib/TGM/plugins.php';
/*
|--------------------------------------------------------------------------
| Includes
|--------------------------------------------------------------------------
|
| Autoload all php files in /includes folder
|
*/
$path = get_template_directory() . '/includes';
autoload_folder($path);
/*
|--------------------------------------------------------------------------
| Settings
|--------------------------------------------------------------------------
|
| Register settings here
|
*/
Wyvern\Includes\Settings::add('New thing', 'new_thing');
Wyvern\Includes\Settings::section('wyvern_tracking_settings', 'Tracking Options', null, 'wyvern_theme_options_tracking');
Wyvern\Includes\Settings::add('Google Tracking ID', 'google_analytics_id', 'input', null, 'wyvern_tracking_settings', 'wyvern_theme_options_tracking');
Wyvern\Includes\Settings::section('wyvern_woocommerce_settings', 'Woocommerce Options', null, 'wyvern_theme_options_woocommerce');
Wyvern\Includes\Settings::add('Variations in table', 'variations_table', 'checkbox', 0, 'wyvern_woocommerce_settings', 'wyvern_theme_options_woocommerce');