-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-orbit-shortcode.php
More file actions
164 lines (115 loc) · 3.67 KB
/
class-orbit-shortcode.php
File metadata and controls
164 lines (115 loc) · 3.67 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
class ORBIT_SHORTCODE extends ORBIT_BASE{
var $shortcode;
var $default_atts;
function __construct(){
add_shortcode( $this->shortcode, array( $this, 'main_shortcode' ), 100 );
add_shortcode( $this->shortcode."_ajax", array( $this, 'ajax_shortcode' ), 100 );
add_action( 'wp_ajax_'.$this->shortcode, array( $this, 'ajax_callback' ) );
add_action( 'wp_ajax_nopriv_'.$this->shortcode, array( $this, 'ajax_callback' ) );
}
function get_default_atts(){
return array();
}
function get_atts( $atts ){
$defaults_atts = apply_filters( $this->shortcode.'_atts', $this->get_default_atts() );
$atts = shortcode_atts( $defaults_atts, $atts, $this->shortcode );
/* ADD URL THAT IS THE EQUIVALENT VERSION OF ALL THE ATTS */
$atts['url'] = $this->get_ajax_url( $atts );
return $atts;
}
function remove_atts_from_cache_key(){
return array('url');
}
function get_cache_key( $atts ){
$atts = $this->get_atts( $atts );
$cache_key = apply_filters('orbit_shortcode_cache_key_str', $this->shortcode);
foreach( $atts as $key => $val ){
if( ! in_array( $key, $this->remove_atts_from_cache_key() ) ){
$cache_key .= $val;
}
}
return md5( $cache_key );
}
function get_cache( $atts ){
$cache_key = $this->get_cache_key( $atts );
// try to get value from Wordpress cache
return get_transient( $cache_key );
}
function set_cache( $data, $atts ){
$cache_key = $this->get_cache_key( $atts );
// store value in cache for hours
set_transient( $cache_key, $data, 3600 * $atts['cache'] );
}
function main_shortcode( $atts, $content ){
$data = false;
if( isset( $atts['cache'] ) && $atts['cache'] && is_numeric( $atts['cache'] ) ){
$data = $this->get_cache( $atts );
}
// if no value in the cache
if ( $data === false ) {
$data = $this->plain_shortcode( $atts, $content );
if( isset( $atts['cache'] ) && $atts['cache'] ){
$this->set_cache( $data, $atts );
}
}
return $data;
}
function plain_shortcode( $atts, $content = false ){
}
/* AJAX CALLBACK FUNCTION */
function ajax_callback(){
/* CREATE SHORTCODE STRING */
$shortcode_str = '['.$this->shortcode;
/* init all attributes for the shortcodes */
foreach($_GET as $key=>$val){
if(isset($_GET[$key])){ $val = $_GET[$key]; }
$shortcode_str .= ' '.$key.'="'.$val.'"';
}
/* CLOSE THE SHORTCODE STRING */
$shortcode_str .= ']';
/* PRINT THE SHORTCODE */
echo do_shortcode( $shortcode_str );
wp_die();
}
function get_ajax_url( $atts ){
return $this->get_ajax_url_from_args( $atts );
}
/* AJAX VERSION OF THE SAME SHORTCODE */
function ajax_shortcode( $atts ){
ob_start();
/* GET ATTRIBUTES FROM THE SHORTCODE */
$atts = $this->get_atts( $atts );
$cache = false;
if( isset( $atts['cache'] ) && $atts['cache'] && is_numeric( $atts['cache'] ) ){
$cache = $this->get_cache( $atts );
}
if ( $cache === false ) {
/* CREATE PARENT ELEMENT THAT WILL HOLD THE AJAX CALLBACK POSTS */
$cache = "<div data-behaviour='oq-reload-html' data-url='".$atts['url']."'></div>";
}
echo $cache;
return ob_get_clean();
}
// CREATE AJAX URL TO REQUEST SUBSEQUENT POSTS LATER
function get_ajax_url_from_args( $args, $dont_include = array() ){
$url = admin_url( 'admin-ajax.php' )."?action=".$this->shortcode;
foreach($args as $key=>$val){
/* CHECK IF THE KEY IS NOT IN THE $DONT_INCLUDE ARRAY */
if(!in_array($key, $dont_include) && $val){ $url .= "&".$key."=".$val;}
}
return $url;
}
function has_shortcode( $posts ){
$found = false;
if ( !empty($posts) ){
foreach ($posts as $post) {
if ( has_shortcode($post->post_content, $this->shortcode ) ){
$found = true;
break;
}
}
}
return $found;
}
}