class-shortcodes-ultimate-shortcodes.php
3.3 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
<?php
/**
* The class responsible for adding, storing and accessing shortcodes data.
*
* @since 5.0.5
* @package Shortcodes_Ultimate
* @subpackage Shortcodes_Ultimate/includes
*/
class Shortcodes_Ultimate_Shortcodes
{
/**
* The collection of available shortcodes.
*
* @since 5.0.5
* @var array
*/
private static $shortcodes = array() ;
/**
* Get all shortcodes.
*
* @since 5.0.5
* @return array The collection of available shortcodes.
*/
public static function get_all()
{
$shortcodes = apply_filters( 'su/data/shortcodes', self::$shortcodes );
$shortcodes = self::add_ids( $shortcodes );
return $shortcodes;
}
/**
* Get specific shortcode by ID.
*
* @since 5.0.5
* @param string $id The ID (without prefix) of shortcode.
* @return array|boolean Shortcode data if found, False otherwise.
*/
public static function get( $id )
{
$shortcodes = self::get_all();
return ( isset( $shortcodes[$id] ) ? $shortcodes[$id] : false );
}
/**
* Add a shortcode.
*
* @since 5.0.5
* @param array $data New shortcode data.
* @param boolean $replace Replace existing shortcode or not.
*/
public static function add( $data = array(), $replace = true )
{
if ( !isset( $data['id'], $data['callback'] ) ) {
trigger_error( 'Shortcode was not added. Missing required params (ID, callback).' );
return;
}
if ( !$replace && self::get( $data['id'] ) ) {
return;
}
self::$shortcodes[$data['id']] = $data;
}
/**
* Remove a shortcode.
*
* @since 5.0.5
* @param string $id Shortcode ID to remove.
*/
public static function remove( $id )
{
if ( isset( self::$shortcodes[$id] ) ) {
unset( self::$shortcodes[$id] );
}
}
/**
* Add built-in shortcodes to the list
*/
public static function add_default_shortcodes()
{
include_once su_get_plugin_path() . 'includes/shortcodes/0-all.php';
}
/**
* Register all available shortcodes.
*
* @since 5.0.5
*/
public static function register()
{
$shortcodes = self::get_all();
$prefix = su_get_shortcode_prefix();
foreach ( $shortcodes as $id => $shortcode ) {
if ( isset( $shortcode['callback'] ) && is_callable( $shortcode['callback'] ) ) {
$callback = $shortcode['callback'];
} elseif ( isset( $shortcode['function'] ) && is_callable( $shortcode['function'] ) ) {
$callback = $shortcode['function'];
} else {
continue;
}
add_shortcode( $prefix . $id, $callback );
}
}
public static function add_ids( $shortcodes )
{
foreach ( $shortcodes as $id => $shortcode ) {
$shortcodes[$id] = array_merge( array(
'id' => $id,
), (array) $shortcode );
}
return $shortcodes;
}
public static function get_groups()
{
$groups = su_get_config( 'groups' );
return apply_filters( 'su/data/groups', $groups );
}
}