Skip to content
Newer
Older
100644 207 lines (175 sloc) 6.37 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
5 Description: Easy ad code management
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';
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
39
40 /**
41 * Instantiate the plugin
42 *
43 * @since ??
44 */
45 function __construct() {
46
47 add_action( 'init', array( &$this, 'action_init' ) );
48 add_action( 'admin_init', array( &$this, 'action_admin_init' ) );
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
49 add_action( 'admin_menu' , array( &$this, 'display_menu' ) );
50 add_action( 'admin_init', array( &$this, 'get_ad_codes' ) );
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
51 }
52
53 /**
54 * Code to run on WordPress' 'init' hook
55 *
56 * @since ??
57 */
58 function action_init() {
59
60 // Allow new domains to be whitelisted
61
62
63 // Ad tags are only run on the frontend
64 if ( !is_admin() ) {
65 require_once AD_CODE_MANAGER_ROOT . '/template-tags.php';
66 add_action( 'acm_tag', array( &$this, 'action_acm_tag' ) );
67 }
68
69 }
70
71 /**
72 * Code to run on WordPress' 'admin_init' hook
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
73 *
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
74 * @since ??
75 */
76 function action_admin_init() {
8d29d6f assets for UI
Rinat Khaziev authored
77 $this->register_scripts_and_styles();
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
78 ///$this->register_ajax_calls();
79 //$this->display_menu();
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
80 // @todo conditionally load the admin interface if that's enabled
81 // The admin interface should be enabled by a filter and off by default
82 // We'll need additional methods for:
83 // - Displaying the interface
84 // - Saving the data
85 // - Loading the ad codes in the database and registering them
86 // with the plugin using
87
88 }
8d29d6f assets for UI
Rinat Khaziev authored
89
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
90 /**
91 * Returns json encoded ad code
92 * This is the datasource for jqGRID
93 *
94 * @todo nonce?
95 * @todo actual logic for getting ad codes from our custom post type
96 */
97 function get_ad_codes() {
98 // These are params that should be managed via UI
99 $response;
100 if ( isset( $_GET[ 'acm-action' ] ) && $_GET[ 'acm-action'] == 'datasource' ) {
101 $model = array(
102 'id' => 1,
103 'site_name' => 'ltv.witi.home',
104 'zone1' => 'homepage',
105 's1' => 'homepage', // does that always mirror zone?
106 'fold' => 'atf', // atf|btf
107 'sz' => '300x250' // 300x250 | 728x90 | 1x1 | 160x600
108 );
109 $return = array();
110 for ( $i = 0; $i < 5; $i++ ) {
111 $model['id'] = $i;
112 $response->rows[$i] = $model;
113 }
114 $count = count( $response->rows );
115 $total_pages = 1; // this should be $count / $_GET[ 'rows' ] // 'rows' is per page limit
116
117 $response->page = isset( $_GET['acm-grid-page'] ) ? $_GET['acm-grid-page'] : 1 ;
118 $response->total = $total_pages;
119 $response->records = $count;
120 $this->print_json( $response );
121 }
122 return;
123 }
124
125 function print_json( $data = array() ) {
126 header( 'Content-type: application/json;' );
127 echo json_encode( $data );
128 exit;
129 }
130
131 /**
132 * Defaults to manage_options, but could be easily changed via acm_manage_ads_cap filter
133 *
134 */
135 function manage_ads_cap() {
136 return apply_filters( 'acm_manage_ads_cap', 'manage_options' );
137 }
138
139 function display_menu() {
140 add_menu_page( $this->title, $this->title, $this->manage_ads_cap(), 'acm', array( &$this, 'admin_view_controller' ) );
141 }
142
143 /**
144 * @todo remove html to views
145 */
146 function admin_view_controller() {
147 ?>
148 <table id="acm-codes-list"></table>
149 <div id="acm-codes-pager"></div>
150 <?php
151 }
8d29d6f assets for UI
Rinat Khaziev authored
152
153 /**
154 * Register scripts and styles
155 *
156 */
157 function register_scripts_and_styles() {
3f73c30 start of jqGrid implementation / basic ui
Rinat Khaziev authored
158 wp_enqueue_style( 'acm-jquery-ui-theme', AD_CODE_MANAGER_URL . '/common/css/jquery-ui-1.8.17.custom.css');
159 wp_enqueue_style( 'acm-jqgrid', AD_CODE_MANAGER_URL . '/common/css/ui.jqgrid.css');
160 wp_enqueue_script( 'acm-jqgrid-locale-en', AD_CODE_MANAGER_URL . '/common/js/grid.locale-en.js', array('jquery', 'jquery-ui-core' ) );
161 wp_enqueue_script( 'acm-jqgrid', AD_CODE_MANAGER_URL . '/common/js/jquery.jqGrid.min.js', array('jquery', 'jquery-ui-core' ) );
162 wp_enqueue_script( 'acm', AD_CODE_MANAGER_URL . '/common/js/acm.js', array('jquery', 'jquery-ui-core' ) );
8d29d6f assets for UI
Rinat Khaziev authored
163 }
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
164
165 /**
166 * Register an ad tag with the plugin so it can be used
167 * on the frontend of the site
168 *
169 * @since ???
170 *
171 * @param string $tag Ad tag for this instance of code
799b6ca @danielbachhuber s/ad tag/ad code/
danielbachhuber authored
172 * @param string $script URL for ad code
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
173 * @param array $where WordPress-style conditionals for where this code should be displayed
174 * @param int $priority What priority this registration runs at
175 * @param array $url_vars Replace tokens in $script with these values
176 * @return bool|WP_Error $success Whether we were successful in registering the ad tag
177 */
799b6ca @danielbachhuber s/ad tag/ad code/
danielbachhuber authored
178 function register_ad_code( $tag, $script, $where = array(), $priority = 10, $url_vars = array() ) {
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
179
180 // @todo Run $script aganist a whitelist to make sure it's a safe URL
181 // @todo Sanitize all of the other input
182
799b6ca @danielbachhuber s/ad tag/ad code/
danielbachhuber authored
183 // @todo logic for saving the ad code to $this->ad_codes so it's available to $this->action_acm_tag()
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
184 }
185
186 /**
187 * Display the ad code based on what's registered
188 * and complicated sorting logic
189 *
190 * @uses do_action( 'acm_tag, 'your_tag_id' )
191 */
192 function action_acm_tag( $tag_id ) {
193
194 // @todo possibly complicated logic for determining which
195 // script is executed while factoring in:
196 // - where it should be displayed
799b6ca @danielbachhuber s/ad tag/ad code/
danielbachhuber authored
197 // - priority against other ad codes
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
198
199 // @todo Parse the script URL and replace with any $url_vars
200
799b6ca @danielbachhuber s/ad tag/ad code/
danielbachhuber authored
201 echo '<script type="text/javascript" src="' . esc_url( $code_url ) . '"></script>';
7e0674e @danielbachhuber Initial commit: skeleton of the plugin
danielbachhuber authored
202
203 }
204
205 }
206 global $ad_code_manager;
207 $ad_code_manager = new Ad_Code_Manager();
Something went wrong with that request. Please try again.