-
Notifications
You must be signed in to change notification settings - Fork 3
/
wpem-rest-api-functions.php
137 lines (123 loc) · 4.25 KB
/
wpem-rest-api-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
<?php
/**
* WPEM Rest Api public functions
* @version 1.0.0
*/
defined( 'ABSPATH' ) || exit;
if( !function_exists( 'wpem_rest_api_prepare_date_response' ) ) {
/**
* Parses and formats a date for ISO8601/RFC3339.
*
* Required WP 4.4 or later.
* See https://developer.wordpress.org/reference/functions/mysql_to_rfc3339/
*
* @since 1.0.0
* @param string|null| $date Date.
* @param bool $utc Send false to get local/offset time.
* @return string|null ISO8601/RFC3339 formatted datetime.
*/
function wpem_rest_api_prepare_date_response( $date, $utc = true ) {
//need improvements as per wpem date time class
return $date;
}
}
if( !function_exists( 'wpem_rest_api_check_post_permissions' ) ) {
/**
* Check permissions of posts on REST API.
*
* @since 1.0.0
* @param string $post_type Post type.
* @param string $context Request context.
* @param int $object_id Post ID.
* @return bool
*/
function wpem_rest_api_check_post_permissions( $post_type, $context = 'read', $object_id = 0 ) {
global $wpdb;
$contexts = array(
'read' => 'read',
'create' => 'publish_posts',
'edit' => 'edit_post',
'delete' => 'delete_post',
'batch' => 'edit_others_posts',
);
$permission = true;
return apply_filters( 'wpem_rest_api_check_permissions', $permission, $context, $object_id, $post_type );
}
}
if( !function_exists( 'wpem_rest_api_urlencode_rfc3986' ) ) {
/**
* Encodes a value according to RFC 3986.
* Supports multidimensional arrays.
*
* @since 1.0.0
* @param string|array $value The value to encode.
* @return string|array Encoded values.
*/
function wpem_rest_api_urlencode_rfc3986( $value ){
if ( is_array( $value ) ) {
return array_map('wpem_rest_api_urlencode_rfc3986', $value);
}
return str_replace( array( '+', '%7E' ), array( ' ', '~' ), rawurlencode( $value ) );
}
}
if( !function_exists( 'wpem_rest_api_color_brightness' ) ) {
/**
* WPEM Color Brightness
*
* @since 1.0.0
* @param string $data Message to be hashed.
* @return string
*/
function wpem_rest_api_color_brightness( $hexCode, $adjustPercent ){
$hexCode = ltrim( $hexCode, '#' );
if ( strlen( $hexCode ) == 3 ) {
$hexCode = $hexCode[0] . $hexCode[0] . $hexCode[1] . $hexCode[1] . $hexCode[2] . $hexCode[2];
}
$hexCode = array_map( 'hexdec', str_split( $hexCode, 2 ) );
foreach ( $hexCode as & $color ) {
$adjustableLimit = $adjustPercent < 0 ? $color : 255 - $color;
$adjustAmount = ceil( $adjustableLimit * $adjustPercent );
$color = str_pad(dechex( $color + $adjustAmount ), 2, '0', STR_PAD_LEFT );
}
return '#' . implode( $hexCode );
}
}
if( !function_exists( 'wpem_rest_api_hex_to_rgb' ) ) {
/**
* WPEM hex to rgb
*
* @since 1.0.0
* @param string $data Message to be hashed.
* @return string
*/
function wpem_rest_api_hex_to_rgb( $colour ){
if ($colour[0] == '#' ) {
$colour = substr($colour, 1);
}
if ( strlen($colour) == 6 ) {
list( $r, $g, $b ) = array( $colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5] );
} elseif ( strlen($colour ) == 3 ) {
list( $r, $g, $b ) = array( $colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2] );
} else {
return false;
}
$r = hexdec( $r );
$g = hexdec( $g );
$b = hexdec( $b );
return array( 'red' => $r, 'green' => $g, 'blue' => $b );
}
}
if( !function_exists( 'wpem_rest_api_check_manager_permissions' ) ) {
/**
* Check manager permissions on REST API.
*
* @since 2.6.0
* @param string $object Object.
* @param string $context Request context.
* @return bool
*/
function wpem_rest_api_check_manager_permissions( $object, $context = 'read' ){
$permission = true;
return apply_filters( 'wpem_rest_api_check_permissions', $permission, $context, 0, $object );
}
}