-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHelpers.php
More file actions
147 lines (126 loc) · 3.25 KB
/
Helpers.php
File metadata and controls
147 lines (126 loc) · 3.25 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
<?php
namespace BEA\PB;
/**
* The purpose of the API class is to have the basic reusable methods like :
* - Template include
* - Template searcher
* - Date formatting
*
* You can put here all of the tools you use in the project but not
* limited to an object or a context.
* It's recommended to use static methods for simple accessing to the methods
* and stick to the non context methods
*
* Class API
* @package BEA\PB
*/
class Helpers {
/**
* Use the trait
*/
use Singleton;
/**
* Locate template in the theme or plugin if needed
*
* @param string $tpl : the tpl name, add automatically .php at the end of the file
*
* @return bool|string
*/
public static function locate_template( string $tpl ) {
if ( empty( $tpl ) ) {
return false;
}
$path = apply_filters( 'beapi_helpers_locate_template_templates', [ 'views/' . BEA_PB_VIEWS_FOLDER_NAME . '/' . $tpl . '.php' ], $tpl, __NAMESPACE__ );
// Locate from the theme
$located = locate_template( $path, false, false );
if ( ! empty( $located ) ) {
return $located;
}
// Locate on the files
if ( is_file( BEA_PB_DIR . 'views/' . $tpl . '.php' ) ) {// Use builtin template
return ( BEA_PB_DIR . 'views/' . $tpl . '.php' );
}
return false;
}
/**
* Include the template given
*
* @param string $tpl : the template name to load
*
* @return bool
*/
public static function include_template( string $tpl ): bool {
if ( empty( $tpl ) ) {
return false;
}
$tpl_path = self::locate_template( $tpl );
if ( false === $tpl_path ) {
return false;
}
include $tpl_path;
return true;
}
/**
* Load the template given and return a view to be render
*
* @param string $tpl : the template name to load
*
* @return \Closure|false
*/
public static function load_template( string $tpl ) {
if ( empty( $tpl ) ) {
return false;
}
$tpl_path = self::locate_template( $tpl );
if ( false === $tpl_path ) {
return false;
}
return static function ( $data ) use ( $tpl_path ) {
if ( ! is_array( $data ) ) {
$data = array( 'data' => $data );
}
// phpcs:ignore WordPress.PHP.DontExtract.extract_extract
extract( $data, EXTR_OVERWRITE );
include $tpl_path;
};
}
/**
* Render a view
*
* @param string $tpl : the template's name
* @param array $data : the template's data
*/
public static function render( string $tpl, $data = array() ): void {
$view = self::load_template( $tpl );
if ( false !== $view ) {
$view( $data );
}
}
/**
* Transform a date to a given format if possible
*
* @param string $date : date to transform
* @param string $from_format : the from date format
* @param string $to_format : the format to transform in
*
* @return string the date formatted
*/
public static function format_date( string $date, string $from_format, string $to_format ): string {
$date = \DateTime::createFromFormat( $from_format, $date );
if ( false === $date ) {
return '';
}
return self::datetime_wp_date( $to_format, $date );
}
/**
* Format on i18
*
* @param string $format
* @param \DateTime $date
*
* @return string
*/
public static function datetime_wp_date( string $format, \DateTime $date ): string {
return wp_date( $format, $date->format( 'U' ) );
}
}