forked from Automattic/_s
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
148 lines (128 loc) · 5.38 KB
/
functions.php
File metadata and controls
148 lines (128 loc) · 5.38 KB
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
<?php
// Optional: change the WP-API prefix from wp-json, so it can work in parallel with the core API.
// Manually flush rewrite rules after changing this constant.
// https://github.com/WP-API/WP-API/issues/1007
define('WP_SOLIDUS_JSON_URL_PREFIX', 'wp-json');
$content_width = 90000;
// Enable redirects for post preview links
function getRedirect($postID) {
// If bloodwater use bloodwater.org else drop subdomain
$domain = preg_match('/bloodwater/',$_SERVER['SERVER_NAME'])?'bloodwater.org':preg_replace('/^(.*?)\.(.*)$/','$2', $_SERVER['SERVER_NAME']);
// Add www unless there is already a subdomain
$domain = preg_match('/^(.*)\.(.*)\.(.*)$/',$domain)?$domain:'www.'.$domain;
if (!is_numeric($postID)) return 'http://'.$domain.$_SERVER["REQUEST_URI"];
$post = get_post($postID);
if ( is_user_logged_in() ) {
$wp_json_nonce = wp_create_nonce('wp_json');
$sample_permalink = get_sample_permalink($post->ID, $post->post_title);
$permalink_pattern = parse_url($sample_permalink[0])['path'];
$redirect_path = preg_replace("/%(.*)%/", $sample_permalink[1], $permalink_pattern); //Put the title slug into the permalink path
$redirect_params = '?_wp_json_nonce='.$wp_json_nonce.'&is_preview=true&post_id='.$postID;
return 'http://'.$domain.$redirect_path.$redirect_params;
} else {
$permalink = parse_url(get_permalink($post->ID));
$redirect_path = $permalink['path'];
return 'http://'.$domain.$redirect_path;
}
}
function setup(){
// Enable support for Post Thumbnails on posts and pages
add_theme_support( 'post-thumbnails' );
// Enable support for Post Formats
add_theme_support( 'post-formats', array( 'image', 'quote', 'link', 'chat', 'audio', 'video' ) );
}
function register_media_taxonomies(){
// Enable category/tag taxonomies for media attachments
register_taxonomy_for_object_type( 'category', 'attachment' );
register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
function add_markdown_support() {
if (function_exists('jetpack_require_lib')) {
jetpack_require_lib( 'markdown' );
}
}
function goto_api_docs(){
wp_redirect( 'http://wp-api.org' );
exit;
}
function redirect_to_posts($url) {
return '/wp-admin/edit.php';
}
function customize_menu(){
remove_menu_page('index.php');
remove_menu_page('edit-comments.php');
remove_menu_page('themes.php');
remove_menu_page('tools.php');
remove_menu_page('edit.php?post_type=feedback');
add_menu_page( 'API', 'API', 'activate_plugins', 'api', 'goto_api_docs', 'dashicons-admin-links', '20.1' );
}
// Add custom fields created by Types plugin to public types_custom_meta key
function add_types_custom_meta($data, $post, $context) {
$types_custom_meta = array();
if (function_exists('types_get_fields')) {
// Toolset Types plugin
$post_custom_data = get_post_custom( $post['ID'] );
// Get a list of custom fields added by Types plugin
$all_types_fields = types_get_fields();
// Create a new array of custom field names
$post_custom_types_fields = array();
foreach ( $all_types_fields as $key => $value ) {
$post_custom_types_fields[] = $value['meta_key'];
}
// Check each post_meta value on post to see if its in the list of Types custom fields
foreach ( $post_custom_data as $key => $value ) {
if ( in_array($key, $post_custom_types_fields) ) {
$types_custom_meta[$key] = $value;
}
}
} else if (function_exists('get_fields')) {
// Advanced Custom Fields plugin, replicate the Toolset Types output
$fields = get_fields( $post['ID'] );
if ( $fields ) {
foreach ( $fields as $key => $value ) {
if ( $key == 'wpcf-retailers' || $key == 'wpcf-tracks' ) {
$values = trim($value);
if ($values) $values = preg_split("/\r\n|\n|\r/", $values);
if ($values) {
$types_custom_meta[$key] = $values;
}
} else if ( $key == 'wpcf-release_date' ) {
$date = DateTime::createFromFormat('d/m/Y H:i:s', $value . ' 00:00:00', new DateTimeZone('UTC'));
if ($date) {
$types_custom_meta[$key] = array();
$types_custom_meta[$key][] = (string) $date->getTimestamp();
}
} else if ($value) {
$types_custom_meta[$key] = array();
$types_custom_meta[$key][] = $value;
}
}
}
}
// If there were any Types custom fields on this post, add them to the API response
if ( !empty($types_custom_meta) ) {
$data['types_custom_meta'] = $types_custom_meta;
}
return $data;
}
function parse_excerpt_markdown( $excerpt ) {
if (class_exists('WPCom_Markdown')) {
$excerpt = WPCom_Markdown::get_instance()->transform( $excerpt );
$excerpt = stripslashes($excerpt);
$excerpt = wpautop($excerpt);
$excerpt = str_replace("\n", "", $excerpt);
}
return $excerpt;
}
add_action( 'init', 'register_media_taxonomies' );
add_action( 'init', 'add_markdown_support' );
add_action( 'after_setup_theme', 'setup' );
add_filter( 'login_redirect', 'redirect_to_posts' );
add_action( 'admin_menu', 'customize_menu', 999 );
add_filter( 'json_prepare_post', 'add_types_custom_meta', 10, 3 );
remove_filter( 'the_excerpt', 'wpautop' ); // Remove this and apply in parse_excerpt_markdown
add_filter( 'get_the_excerpt', 'parse_excerpt_markdown' );
add_filter( 'json_url_prefix', function( $prefix ) { return WP_SOLIDUS_JSON_URL_PREFIX; } );
if( class_exists( 'WpeCommon' ) ){
require 'wpengine.php';
}