-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdfads.php
More file actions
237 lines (198 loc) · 6.53 KB
/
dfads.php
File metadata and controls
237 lines (198 loc) · 6.53 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/*
Plugin Name: Ads by datafeedr.com
Plugin URI: https://www.datafeedr.com/
Description: Randomly display any type of advertisement anywhere on your site. Add rotating banner ads, Google Adsense, videos, text links and more to your sidebar, widget areas, posts and pages.
Version: 1.2.0
Tested up to: 6.3.3-alpha
Author: datafeedr.com
Author URI: https://www.datafeedr.com/
Copyright 2023 Ads by datafeedr.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
define( 'DFADS_PLUGIN_PATH', plugin_dir_path(__FILE__) );
define( 'DFADS_PLUGIN_URL', plugin_dir_url(__FILE__) );
define( 'DFADS_METABOX_PREFIX', '_dfads_' );
define( 'DFADS_CONTEXT', 'dfads' );
define( 'DFADS_DOCS_URL', 'http://www.datafeedr.com/dfads/' );
define( 'DFADS_VERSION', '1.2.0' );
/**
* Require necessary files.
*/
require_once( DFADS_PLUGIN_PATH . 'inc/functions.php' );
require_once( DFADS_PLUGIN_PATH . 'inc/cpt.class.php' );
require_once( DFADS_PLUGIN_PATH . 'inc/dfads.class.php' );
if (is_admin()) {
require_once( DFADS_PLUGIN_PATH . 'inc/admin.php' );
require_once( DFADS_PLUGIN_PATH . 'inc/metaboxes.class.php' );
}
// Load jQuery on front end pages.
function dfads_load_js() {
if ( !is_admin() ) {
wp_enqueue_script( 'jquery' );
}
}
add_action( 'init', 'dfads_load_js' );
/**
* Instantiate the DFADS class.
*/
function dfads( $args='' ) {
$ads = new DFADS();
if ( is_array( $args ) ) {
$args = http_build_query( $args );
}
return $ads->get_ads( $args );
}
/**
* WP Ajax for front and backend.
*/
function dfads_ajax_load_ads(){
$request = $_REQUEST;
$user_signature = trim( $request['signature'] ?? '' );
if ( empty( $user_signature ) ) {
die( 'Missing signature' );
}
unset( $request['signature'] );
$allowed_params = array_keys( DFADS::get_default_params() );
foreach ( $request as $k => $v ) {
// These are added here: DFADS::get_javascript();
if ( in_array( $k, [ '_block_id', 'action' ] ) ) {
continue;
}
// Remove any other keys provided that aren't allowed.
if ( ! in_array( $k, $allowed_params, true ) ) {
unset( $request[ $k ] );
}
}
$known_signature = dfads_hash_hmac( serialize( $request ) );
if ( ! hash_equals( $known_signature, $user_signature ) ) {
die( 'Invalid signature' );
}
echo dfads( $request );
die;
}
add_action('wp_ajax_nopriv_dfads_ajax_load_ads', 'dfads_ajax_load_ads');
add_action('wp_ajax_dfads_ajax_load_ads', 'dfads_ajax_load_ads');
/**
* Add shortcode functionality to Text widget.
*/
function dfads_enable_shortcodes_in_text_widget() {
$output = get_option( 'dfads-settings' );
if ( isset( $output['dfads_enable_shortcodes_in_widgets'] ) ) {
add_filter( 'widget_text', 'do_shortcode' );
}
}
add_action( 'init', 'dfads_enable_shortcodes_in_text_widget' );
/**
* Add _dfads_impression_count custom field to new post.
*/
function dfads_save_post( $post_id ) {
$slug = 'dfads';
$_POST += array("{$slug}_edit_nonce" => '');
if ( isset( $_POST['post_type'] ) && $slug != $_POST['post_type'] ) { return; }
if ( !current_user_can( 'edit_post', $post_id ) ) { return; }
if ( !wp_verify_nonce( $_POST["{$slug}_edit_nonce"], plugin_basename( __FILE__ ) ) ) { return; }
if ( isset( $post_id ) ) {
add_post_meta($post_id, DFADS_METABOX_PREFIX.'impression_count', 0);
}
}
add_action( 'save_post', 'dfads_save_post' );
/**
* Add shortcode functionality.
*/
function dfads_shortcode( $atts ) {
return dfads( $atts['params'] );
}
add_shortcode( 'dfads', 'dfads_shortcode' );
/**
* Add settings link on plugin page.
*/
function dfads_settings_link( $links ) {
$settings_link = '<a href="options-general.php?page=dfads-settings">Settings</a>';
array_unshift( $links, $settings_link) ;
return $links;
}
add_filter( 'plugin_action_links_'.plugin_basename(__FILE__), 'dfads_settings_link' );
/**
* Add plugin meta links on plugin page.
*/
function dfads_plugin_row_meta( $links, $file ) {
$plugin = plugin_basename( __FILE__ );
if ( $file == $plugin ) {
return array_merge(
$links,
array( '<a href="'.DFADS_DOCS_URL.'">Documentation</a>' )
);
}
return $links;
}
add_filter( 'plugin_row_meta', 'dfads_plugin_row_meta', 10, 2 );
/**
* Add help drop down menu to plugin's settings page.
*/
function dfads_help( $contextual_help, $screen_id, $screen ) {
global $dfads_plugin_hook;
if ($screen_id == $dfads_plugin_hook) {
$contextual_help = '<p><a href="'.DFADS_DOCS_URL.'">Documentation</a></p>';
}
return $contextual_help;
}
//add_filter( 'contextual_help', 'dfads_help', 10, 3 );
/**
* Remove "Visual" editor from "Add Ad" page.
*/
function dfads_user_can_richedit( $default ) {
global $post;
if ( DFADS_CONTEXT == get_post_type( $post ) ) {
return false;
}
return $default;
}
add_filter( 'user_can_richedit', 'dfads_user_can_richedit' );
/**
* Hide some meta boxes from "Add Ad" page.
*/
function dfads_default_hidden_meta_boxes( $hidden, $screen ) {
if ( DFADS_CONTEXT == $screen->id ) {
$hidden[] = 'postcustom'; //hide custom fields.
$hidden[] = 'pageparentdiv'; //hide page attributes fields.
$hidden[] = 'postimagediv'; // hide featured image.
$hidden[] = 'revisionsdiv'; // hide revisions.
}
return $hidden;
}
add_filter( 'default_hidden_meta_boxes', 'dfads_default_hidden_meta_boxes', 10, 2 );
/**
* Delete transient data related to DFADS
* https://github.com/Seebz/Snippets/tree/master/Wordpress/plugins/purge-transients
*/
function dfads_purge_transients() {
global $wpdb;
$older_than='10 seconds';
$older_than_time = strtotime( '-' . $older_than );
if ( $older_than_time > time() || $older_than_time < 1 ) {
return false;
}
$transients = $wpdb->get_col(
$wpdb->prepare( "
SELECT REPLACE(option_name, '_transient_timeout_', '') AS transient_name
FROM {$wpdb->options}
WHERE option_name LIKE '\_transient\_timeout\_dfad\__%%'
AND option_value < %s
", $older_than_time )
);
foreach($transients as $transient) {
get_transient( $transient );
}
update_option('dfads_transient_data_deleted_time', time());
}
add_action( 'shutdown', 'dfads_purge_transients' );