forked from bshiluk/vue-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
170 lines (132 loc) · 4.33 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
<?php
/**
* Set up theme options on activation
*/
function vue_wordpress_setup()
{
add_theme_support( 'title-tag' );
add_theme_support( 'post-thumbnails' );
add_theme_support( 'custom-logo', array(
'height' => 160,
'width' => 160,
) );
register_nav_menus( array(
'main' => 'Main Menu',
) );
}
add_action( 'after_setup_theme', 'vue_wordpress_setup' );
/**
* Load scripts and styles
*/
function vue_wordpress_scripts()
{
// Styles
wp_enqueue_style( 'style.css', get_template_directory_uri() . '/style.css' );
wp_enqueue_style('vue_wordpress.css', get_template_directory_uri() . '/dist/vue-wordpress.css');
// Scripts
// Enable For Production - Disable for Development
wp_enqueue_script('vue_wordpress.js', get_template_directory_uri() . '/dist/vue-wordpress.js', array(), null, true);
// Enable For Development - Remove for Production
// wp_enqueue_script( 'vue_wordpress.js', 'http://localhost:8080/vue-wordpress.js', array(), false, true );
}
add_action( 'wp_enqueue_scripts', 'vue_wordpress_scripts' );
/**
* Declare REST API Data Localizer dependency
*/
if ( !class_exists( 'RADL' ) ) {
add_action( 'admin_notices', function () {
echo '<div class="error"><p>REST API Data Localizer not activated. To use this theme go to <a href="' . esc_url( admin_url( 'plugins.php' ) ) . '">plugins</a> to download and/or activate REST API Data Localizer.</p></div>';
} );
return;
}
/**
* Initialize REST API Data Localizer
*/
new RADL( '__VUE_WORDPRESS__', 'vue_wordpress.js', array(
'routing' => RADL::callback( 'vue_wordpress_routing' ),
'state' => array(
'categories' => RADL::endpoint( 'categories'),
'media' => RADL::endpoint( 'media' ),
'menus' => RADL::callback( 'vue_wordpress_menus' ),
'pages' => RADL::endpoint( 'pages' ),
'posts' => RADL::endpoint( 'posts' ),
'tags' => RADL::endpoint( 'tags' ),
'users' => RADL::endpoint( 'users' ),
'site' => RADL::callback( 'vue_wordpress_site' ),
),
) );
/**
* REST API Data Localizer callbacks
*/
function vue_wordpress_routing()
{
$routing = array(
'category_base' => get_option( 'category_base' ),
'page_on_front' => null,
'page_for_posts' => null,
'permalink_structure' => get_option( 'permalink_structure' ),
'show_on_front' => get_option( 'show_on_front' ),
'tag_base' => get_option( 'tag_base' ),
'url' => get_bloginfo( 'url' )
);
if ( $routing['show_on_front'] === 'page' ) {
$front_page_id = get_option( 'page_on_front' );
$posts_page_id = get_option( 'page_for_posts' );
if ( $front_page_id ) {
$front_page = get_post( $front_page_id );
$routing['page_on_front'] = $front_page->post_name;
}
if ( $posts_page_id ) {
$posts_page = get_post( $posts_page_id );
$routing['page_for_posts'] = $posts_page->post_name;
}
}
return $routing;
}
function vue_wordpress_menus()
{
$menus = array();
// $locations is an array where ([NAME] = MENU_ID);
$locations = get_nav_menu_locations();
foreach ( array_keys( $locations ) as $name ) {
$id = $locations[$name];
$menu = array();
$menu_items = wp_get_nav_menu_items( $id );
foreach ( $menu_items as $i ) {
array_push( $menu, array(
'id' => $i->ID,
'parent' => $i->menu_item_parent,
'target' => $i->target,
'content' => $i->title,
'title' => $i->attr_title,
'url' => $i->url,
) );
}
$menus[$name] = $menu;
}
return $menus;
}
function vue_wordpress_site()
{
return array(
'description' => get_bloginfo( 'description' ),
'docTitle' => '',
'loading' => false,
'logo' => get_theme_mod( 'custom_logo' ),
'name' => get_bloginfo( 'name' ),
'posts_per_page' => get_option( 'posts_per_page' ),
'url' => get_bloginfo( 'url' )
);
}
/**
* In template functions
*/
function vue_wordpress_min_read( $content )
{
$length = count( explode( ' ', $content ) ) + 1;
$time = $length / 200;
if ( is_float( $time ) ) {
$time = ceil( $time );
}
return $time . 'min read';
}