Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 391 lines (347 sloc) 12.581 kB
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
1 <?php
2 /*
3 Plugin Name: Ad Code Manager
4 Plugin URI: http://automattic.com
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
5 Description: Easy ad code management
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
6 Author: Daniel Bachhuber, Rinat Khaziev, Automattic
7 Version: 0.0
8 Author URI: http://automattic.com
9
10 GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
26 */
27 define( 'AD_CODE_MANAGER_VERSION', '0.0' );
28 define( 'AD_CODE_MANAGER_ROOT' , dirname(__FILE__) );
29 define( 'AD_CODE_MANAGER_FILE_PATH' , AD_CODE_MANAGER_ROOT . '/' . basename(__FILE__) );
30 define( 'AD_CODE_MANAGER_URL' , plugins_url( plugin_basename(dirname(__FILE__)).'/') );
31
32 class Ad_Code_Manager
33 {
34
799b6ca @danielbachhuber s/ad tag/ad code/
danielbachhuber authored
35 var $ad_codes = array();
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
36 var $script_url_whitelist = array();
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
37 var $title = 'Ad Code Manager';
38 var $post_type = 'acm-code';
5a0badc register scripts and styles conditionally on admin_enqueue_scripts
rinatkhaziev authored
39 var $plugin_slug = 'acm';
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
40 var $post_type_labels ;
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
41 /**
42 * Instantiate the plugin
43 *
44 * @since ??
45 */
46 function __construct() {
9c8abe6 UI
Rinat Khaziev authored
47 // @todo refactor TODO
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
48 add_action( 'init', array( &$this, 'action_init' ) );
49 add_action( 'admin_init', array( &$this, 'action_admin_init' ) );
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
50 add_action( 'admin_menu' , array( &$this, 'display_menu' ) );
5401576 formatting
rinatkhaziev authored
51 add_action( 'admin_init', array( &$this, 'get_ad_codes' ) );
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
52 add_action( 'admin_init', array( &$this, 'ad_code_edit_actions' ) );
53 add_action( 'admin_init', array( &$this, 'conditions_edit_actions' ) );
9c8abe6 UI
Rinat Khaziev authored
54 add_action( 'admin_init', array( &$this, 'get_conditions' ) );
5a0badc register scripts and styles conditionally on admin_enqueue_scripts
rinatkhaziev authored
55 add_action( 'admin_enqueue_scripts', array( &$this, 'register_scripts_and_styles') );
475678f remove hardcode in acm.js
Rinat Khaziev authored
56 add_action( 'admin_print_scripts', array( &$this, 'post_admin_header' ) );
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
57
58 $this->post_type_labels = array(
59 'name' => __( 'DFP Ad Codes' ),
60 'singular_name' => __( 'DFP Ad Codes' )
61 );
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
62 }
63
64 /**
65 * Code to run on WordPress' 'init' hook
66 *
67 * @since ??
68 */
69 function action_init() {
70
71 // Allow new domains to be whitelisted
72
73
74 // Ad tags are only run on the frontend
75 if ( !is_admin() ) {
76 require_once AD_CODE_MANAGER_ROOT . '/template-tags.php';
77 add_action( 'acm_tag', array( &$this, 'action_acm_tag' ) );
78 }
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
79 $this->register_acm_post_type();
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
80
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
81 }
82
83 /**
84 * Code to run on WordPress' 'admin_init' hook
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
85 *
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
86 * @since ??
87 */
88 function action_admin_init() {
89 // @todo conditionally load the admin interface if that's enabled
90 // The admin interface should be enabled by a filter and off by default
91 // We'll need additional methods for:
92 // - Displaying the interface
93 // - Saving the data
94 // - Loading the ad codes in the database and registering them
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
95 // with the plugin using
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
96 }
97
98 /**
99 * Register our custom post type to store ad codes
100 *
101 * @since ??
102 */
103 function register_acm_post_type() {
104 register_post_type( $this->post_type, array(
105 'labels' => $this->post_type_labels,
106 'public' => false
107 ) );
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
108 }
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
109
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
110 /**
111 * Returns json encoded ad code
112 * This is the datasource for jqGRID
113 *
114 * @todo nonce?
115 * @todo actual logic for getting ad codes from our custom post type
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
116 */
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
117 function get_ad_codes() {
118 // These are params that should be managed via UI
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
119 /**
120 * NB!
121 * $response is an object with following properties
122 * $response->page = current page
123 * $response->total = total pages
124 * $response->record = count of rows
125 * $response->rows = nested array of assoc arrays @see $model
126 */
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
127 $response;
128 if ( isset( $_GET[ 'acm-action' ] ) && $_GET[ 'acm-action'] == 'datasource' ) {
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
129
130 $ad_codes = get_posts( array(
131 'post_type' => $this->post_type
132 ) );
133 // prepare data in jqGrid specific format
134 $pass = array();
135 foreach ( $ad_codes as $ad_code ) {
136 $pass[] = array(
137 'id' => $ad_code->ID,
138 'site_name' => get_post_meta( $ad_code->ID, 'site_name', true ),
139 'zone1' => get_post_meta( $ad_code->ID, 'zone1', true ),
140 's1' => get_post_meta( $ad_code->ID, 'zone1', true ),
141 'act' => ''
142 );
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
143 }
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
144 $response->rows = $pass;
145
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
146 $count = count( $response->rows );
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
147 $total_pages = 1; // this should be $count / $_GET[ 'rows' ] // 'rows' is per page limit
148
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
149 $response->page = isset( $_GET['acm-grid-page'] ) ? $_GET['acm-grid-page'] : 1 ;
150 $response->total = $total_pages;
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
151 $response->records = $count;
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
152 $this->print_json( $response );
153 }
154 return;
155 }
9c8abe6 UI
Rinat Khaziev authored
156
157 function get_conditions() {
158 if ( isset( $_GET[ 'acm-action' ] ) && $_GET[ 'acm-action'] == 'datasource-conditions' & 0 !== intval( $_GET[ 'id' ] ) ) {
159 $response;
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
160 $conditions = get_post_meta( intval( $_GET[ 'id' ] ), 'conditions', true);
161 foreach ($conditions as $index => $item )
162 $response->rows[] = $item;
9c8abe6 UI
Rinat Khaziev authored
163 $count = count( $response->rows );
164 $total_pages = 1; // this should be $count / $_GET[ 'rows' ] // 'rows' is per page limit
165
166 $response->page = isset( $_GET['acm-grid-page'] ) ? $_GET['acm-grid-page'] : 1 ;
167 $response->total = $total_pages;
168 $response->records = $count;
169 $this->print_json( $response );
170 }
171 return;
172 }
173
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
174 /**
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
175 * Handles Create, Update, Delete actions
176 *
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
177 * @todo nonce + jqGrid?
178 */
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
179 function ad_code_edit_actions() {
9c8abe6 UI
Rinat Khaziev authored
180 if ( isset( $_GET[ 'acm-action' ] ) && $_GET[ 'acm-action'] == 'edit' && ! empty( $_POST ) ) {
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
181 switch( $_POST[ 'oper' ] ) { //this is jqGrid param
182 case 'add':
183 $this->create_ad_code();
184 break;
185 case 'edit':
186 $this->edit_ad_code();
187 break;
188 case 'del':
189 $this->delete_ad_code();
190 break;
191 }
192 exit; // exit, jqGrid sends another request to fetch new data
193 }
194 return;
195 }
196
197 function conditions_edit_actions() {
198 if ( isset( $_GET[ 'acm-action' ] ) && $_GET[ 'acm-action'] == 'edit-conditions' && ! empty( $_POST ) ) {
199 switch( $_POST[ 'oper' ] ) { //this is jqGrid param
200 case 'add':
201 $this->create_condition();
202 break;
203 case 'edit':
204 $this->edit_condition();
205 break;
206 case 'del':
207 $this->delete_condition();
208 break;
209 }
210 exit;
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
211 }
212 return;
213 }
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
214
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
215 /**
216 * @uses register_ad_code()
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
217 * @todo validation / nonce
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
218 */
219 function create_ad_code() {
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
220 if ( $_POST['site_name'] && $_POST['zone1'] ) {
221 $acm_post = array(
222 'post_title' => $_POST['site_name'] .'-' . $_POST['zone1'],
223 'post_status' => 'publish',
224 'comment_status' => 'closed',
225 'ping_status' => 'closed',
226 'post_type' => $this->post_type
227 );
228 if ( !is_wp_error( $acm_inserted_post_id = wp_insert_post( $acm_post, true) ) ) { // ??
229 update_post_meta( $acm_inserted_post_id, 'site_name', $_POST['site_name'] );
230 update_post_meta( $acm_inserted_post_id, 'zone1', $_POST['zone1'] );
231 update_post_meta( $acm_inserted_post_id, 's1', $_POST['zone1'] ); // to remove
232 }
233 }
234 return;
235 }
236
237 function edit_ad_code() {
238 if ( isset($_POST['id'] ) && $_POST['site_name'] && $_POST['zone1'] ) {
239 $acm_inserted_post_id = intval( $_POST[ 'id' ] );
240 update_post_meta( $acm_inserted_post_id, 'site_name', $_POST['site_name'] );
241 update_post_meta( $acm_inserted_post_id, 'zone1', $_POST['zone1'] );
242 update_post_meta( $acm_inserted_post_id, 's1', $_POST['zone1'] ); // to remove
243 }
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
244 return;
245 }
246
247 function delete_ad_code() {
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
248 if ( isset( $_POST['id'] ) )
249 wp_delete_post( intval( $_POST[ 'id' ] ) , true ); //force delete post
250 return;
251 }
252
253 function create_condition() {
254 if ( isset( $_GET['id'] ) && !empty( $_POST ) ) {
255 $existing_conditions = get_post_meta( intval( $_GET[ 'id' ] ), 'conditions', true );
256 if ( ! is_array( $existing_conditions) ) {
257 $existing_conditions = array();
258 }
259 $existing_conditions[] = array(
260 'condition' => $_POST[ 'condition' ],
261 'value' => $_POST[ 'value' ],
262 'priority'=> intval( $_POST[ 'priority' ] )
263 );
264 update_post_meta( intval( $_GET[ 'id' ] ), 'conditions', $existing_conditions );
265 }
266 return;
267 }
268
269 function edit_condition() {
270 if ( isset( $_GET['id'] ) && !empty( $_POST ) ) {
271 $existing_conditions = (array) get_post_meta( intval( $_GET[ 'id' ] ), 'conditions', true );
272
273 foreach ( $existing_conditions as $index => $condition ) {
274 if ( $_POST[ 'condition' ] == $condition[ 'condition' ] ) {
275 $existing_conditions[ $index ] = array(
276 'condition' => $_POST[ 'condition' ],
277 'value' => $_POST[ 'value' ],
278 'priority'=> intval( $_POST[ 'priority' ] ) );
279 }
280 }
281 update_post_meta( intval( $_GET[ 'id' ] ), 'conditions', $existing_conditions );
282 }
283 return;
284 }
285
286 function delete_condition() {
287 if ( isset( $_GET['id'] ) && !empty( $_POST ) ) {
288 $existing_conditions = get_post_meta( intval( $_GET[ 'id' ] ), 'conditions', true );
289 $ids_to_delete = explode(",", $_POST[ 'id' ] ); //
290 foreach ($ids_to_delete as $index )
291 unset( $existing_conditions[ --$index ] ); // jqGrid starts with one, PHP starts with 0
292 update_post_meta( intval( $_GET[ 'id' ] ), 'conditions', array_values( $existing_conditions ) ); //array_values to keep indices consistent
293 }
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
294 return;
295 }
296
297 /**
298 * encode as json any given $data
299 */
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
300 function print_json( $data = array() ) {
301 header( 'Content-type: application/json;' );
302 echo json_encode( $data );
303 exit;
304 }
475678f remove hardcode in acm.js
Rinat Khaziev authored
305 /**
306 * Print our vars as JS
307 */
308 function post_admin_header() {
309 ?>
310 <script type="text/javascript">
311 var acm_url = '<?php echo esc_js( admin_url('admin.php?page=' . $this->plugin_slug ) ) ?>';
312 </script>
313 <?php
314 }
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
315
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
316 function display_menu() {
5a0badc register scripts and styles conditionally on admin_enqueue_scripts
rinatkhaziev authored
317 add_menu_page( $this->title, $this->title, apply_filters( 'acm_manage_ads_cap', 'manage_options' ), $this->plugin_slug, array( &$this, 'admin_view_controller' ) );
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
318 }
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
319
320 /**
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
321 * @todo remove html to views
322 */
323 function admin_view_controller() {
324 ?>
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
325 <h2>Ad Code Manager</h2>
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
326 <table id="acm-codes-list"></table>
327 <div id="acm-codes-pager"></div>
2b607c1 create, read, update, delete logic for ad codes and conditions. Fully…
Rinat Khaziev authored
328
8b01a2c implement the js part of conditions interface
Rinat Khaziev authored
329 <table id="acm-codes-conditions-list"></table>
330 <div id="acm-codes-conditions-pager"></div>
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
331 <?php
332 }
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
333
8d29d6f assets for UI
Rinat Khaziev authored
334 /**
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
335 * Register scripts and styles
8d29d6f assets for UI
Rinat Khaziev authored
336 *
337 */
338 function register_scripts_and_styles() {
41a1171 check against global
Rinat Khaziev authored
339 global $pagenow;
340 if ( 'admin.php' == $pagenow && isset( $_GET['page'] ) && $_GET['page'] == $this->plugin_slug ) {
5a0badc register scripts and styles conditionally on admin_enqueue_scripts
rinatkhaziev authored
341 wp_enqueue_style( 'acm-jquery-ui-theme', AD_CODE_MANAGER_URL . '/common/css/jquery-ui-1.8.17.custom.css');
342 wp_enqueue_style( 'acm-jqgrid', AD_CODE_MANAGER_URL . '/common/css/ui.jqgrid.css');
343 wp_enqueue_script( 'acm-jqgrid-locale-en', AD_CODE_MANAGER_URL . '/common/js/grid.locale-en.js', array('jquery', 'jquery-ui-core' ) );
344 wp_enqueue_script( 'acm-jqgrid', AD_CODE_MANAGER_URL . '/common/js/jquery.jqGrid.min.js', array('jquery', 'jquery-ui-core' ) );
345 wp_enqueue_script( 'acm', AD_CODE_MANAGER_URL . '/common/js/acm.js', array('jquery', 'jquery-ui-core' ) );
346 }
8d29d6f assets for UI
Rinat Khaziev authored
347 }
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
348
349 /**
350 * Register an ad tag with the plugin so it can be used
351 * on the frontend of the site
352 *
353 * @since ???
354 *
355 * @param string $tag Ad tag for this instance of code
799b6ca @danielbachhuber s/ad tag/ad code/
danielbachhuber authored
356 * @param string $script URL for ad code
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
357 * @param array $where WordPress-style conditionals for where this code should be displayed
358 * @param int $priority What priority this registration runs at
359 * @param array $url_vars Replace tokens in $script with these values
360 * @return bool|WP_Error $success Whether we were successful in registering the ad tag
361 */
799b6ca @danielbachhuber s/ad tag/ad code/
danielbachhuber authored
362 function register_ad_code( $tag, $script, $where = array(), $priority = 10, $url_vars = array() ) {
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
363
364 // @todo Run $script aganist a whitelist to make sure it's a safe URL
365 // @todo Sanitize all of the other input
366
799b6ca @danielbachhuber s/ad tag/ad code/
danielbachhuber authored
367 // @todo logic for saving the ad code to $this->ad_codes so it's available to $this->action_acm_tag()
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
368 }
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
369
370 /**
371 * Display the ad code based on what's registered
372 * and complicated sorting logic
373 *
374 * @uses do_action( 'acm_tag, 'your_tag_id' )
375 */
376 function action_acm_tag( $tag_id ) {
a35d1e8 skeleton CRUD actions, implemented inline cell editing for jqGrid
rinatkhaziev authored
377
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
378 // @todo possibly complicated logic for determining which
379 // script is executed while factoring in:
380 // - where it should be displayed
799b6ca @danielbachhuber s/ad tag/ad code/
danielbachhuber authored
381 // - priority against other ad codes
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
382
383 // @todo Parse the script URL and replace with any $url_vars
384
799b6ca @danielbachhuber s/ad tag/ad code/
danielbachhuber authored
385 echo '<script type="text/javascript" src="' . esc_url( $code_url ) . '"></script>';
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
386
387 }
388
389 }
390 global $ad_code_manager;
391 $ad_code_manager = new Ad_Code_Manager();
Something went wrong with that request. Please try again.