Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbachhuber committed Jan 19, 2012
2 parents 528c846 + 0d0e65c commit 41cc15f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 30 deletions.
75 changes: 48 additions & 27 deletions ad-code-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Plugin URI: http://automattic.com
Description: Easy ad code management
Author: Daniel Bachhuber, Rinat Khaziev, Automattic
Version: 0.1
Version: 0.1.1
Author URI: http://automattic.com
GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
Expand All @@ -24,10 +24,10 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
define( 'AD_CODE_MANAGER_VERSION', '0.1' );
define( 'AD_CODE_MANAGER_VERSION', '0.1.1' );
define( 'AD_CODE_MANAGER_ROOT' , dirname( __FILE__ ) );
define( 'AD_CODE_MANAGER_FILE_PATH' , AD_CODE_MANAGER_ROOT . '/' . basename( __FILE__ ) );
define( 'AD_CODE_MANAGER_URL' , plugins_url( plugin_basename( dirname( __FILE__ ) ) . '/' ) );
define( 'AD_CODE_MANAGER_URL' , plugins_url( '/', __FILE__ ) );

class Ad_Code_Manager
{
Expand Down Expand Up @@ -126,7 +126,7 @@ function action_init() {
'tag' => '300x250-btf',
'url_vars' => array(
'sz' => '300x250',
'fold' => 'atf'
'fold' => 'btf'
)
),
array(
Expand All @@ -135,7 +135,7 @@ function action_init() {
'sz' => '160x600',
'fold' => 'atf'
)
)
),
);
$this->ad_tag_ids = apply_filters( 'acm_ad_tag_ids', $this->ad_tag_ids );

Expand Down Expand Up @@ -165,10 +165,10 @@ function register_acm_post_type() {
*
* @since 0.1
*/
function ajax_handler() {
function ajax_handler() {
if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'acm_nonce' ) )
return;

if ( !current_user_can( $this->manage_ads_cap ) )
return;

Expand All @@ -192,8 +192,7 @@ function ajax_handler() {
/**
* Returns json encoded ad code
* This is the datasource for jqGRID
*
* @todo nonce?
*
*/
function get_ad_codes_ajax() {
// These are params that should be managed via UI
Expand All @@ -203,11 +202,18 @@ function get_ad_codes_ajax() {
* $response->page = current page
* $response->total = total pages
* $response->record = count of rows
* $response->rows = nested array of assoc arrays @see $model
* $response->rows = nested array of assoc arrays
*/
$response;
if ( isset( $_GET[ 'acm-action' ] ) && $_GET[ 'acm-action'] == 'datasource' ) {
$ad_codes = $this->get_ad_codes() ;
$response->page = isset( $_GET[ 'acm-grid-page' ] ) ? sanitize_key( $_GET[ 'acm-grid-page' ] ) : 1 ;
$query_args = array();

// We need to pass offset to get_ad_codes offset for jqGrid to work correctly
if ( 1 < $response->page )
$query_args['offset'] = ( $response->page - 1 ) * intval( $_GET['rows'] );

$ad_codes = $this->get_ad_codes( $query_args ) ;
// prepare data in jqGrid specific format
$pass = array();
foreach ( $ad_codes as $ad_code ) {
Expand All @@ -219,13 +225,10 @@ function get_ad_codes_ajax() {
);
}
$response->rows = $pass;

$count = count( $response->rows );
$total_pages = 1; // this should be $count / $_GET[ 'rows' ] // 'rows' is per page limit

$response->page = isset( $_GET[ 'acm-grid-page' ] ) ? sanitize_key( $_GET[ 'acm-grid-page' ] ) : 1 ;
$response->total = $total_pages;
$response->records = $count;
$count_object = wp_count_posts( $this->post_type );
$total_pages = ceil ( $count_object->publish / $_GET['rows'] );
$response->total = $total_pages;
$response->records = $count_object->publish;
$this->print_json( $response );
}
return;
Expand All @@ -237,12 +240,23 @@ function get_ad_codes_ajax() {
*
* @todo This is too DFP specific. Abstract it
*/
function get_ad_codes() {
function get_ad_codes( $query_args = array() ) {
$ad_codes_formatted = array();
$allowed_query_params = apply_filters( 'acm_allowed_get_posts_args', array( 'offset' ) );

$args = array(
'post_type' => $this->post_type,
'numberposts' => apply_filters( 'acm_ad_code_count', 50 ),
);

foreach ( (array) $query_args as $query_key => $query_value ) {
if ( ! in_array( $query_key, $allowed_query_params ) ) {
unset( $query_args[$query_key] );
} else {
$args[$query_key] = $query_value;
}
}

$ad_codes = get_posts( $args );
foreach ( $ad_codes as $ad_code_cpt ) {
$ad_codes_formatted[] = array(
Expand All @@ -260,13 +274,17 @@ function get_ad_codes() {
function get_conditionals_ajax() {
if ( 0 !== intval( $_GET[ 'id' ] ) ) {
$conditionals = $this->get_conditionals( intval( $_GET[ 'id' ] ) );
$response;
foreach ( $conditionals as $index => $item )
$response;
foreach ( $conditionals as $index => $item ) {
if ( is_array( $item['arguments'] ) ) {
$item['arguments'] = implode(";", $item['arguments'] );
}
$response->rows[] = $item;
}
$count = count( $response->rows );
$total_pages = 1; // this should be $count / $_GET[ 'rows' ] // 'rows' is per page limit
$total_pages = ceil ( $count / $_GET['rows'] );

$response->page = isset( $_GET['acm-grid-page'] ) ? sanitize_key( $_GET['acm-grid-page'] ) : 1 ;
$response->page = isset( $_GET['acm-grid-page'] ) ? sanitize_text_field( $_GET['acm-grid-page'] ) : 1 ;
$response->total = $total_pages;
$response->records = $count;
$this->print_json( $response );
Expand Down Expand Up @@ -311,7 +329,8 @@ function conditionals_edit_actions() {
if ( ! empty( $_POST ) ) {
$conditional_vals = array(
'function' => sanitize_key( $_POST['function'] ),
'arguments' => array_map( 'sanitize_text_field', $_POST['arguments'] ),
//arguments from jqGrid are passed as string, need to check arguments type before choosing the way to sanitize the value
'arguments' => is_array( $_POST['arguments'] ) ? array_map( 'sanitize_text_field', $_POST['arguments'] ) : sanitize_text_field( $_POST['arguments'] ),
);
switch ( $_POST[ 'oper' ] ) {
case 'add':
Expand Down Expand Up @@ -392,7 +411,7 @@ function create_conditional( $ad_code_id, $conditional ) {
}
$existing_conditionals[] = array(
'function' => $conditional[ 'function' ],
'arguments' => (array) $conditional[ 'arguments' ], // @todo explode
'arguments' => explode(';', $conditional[ 'arguments' ] ), // @todo filterize explode character?
);
update_post_meta( $ad_code_id, 'conditionals', $existing_conditionals );
}
Expand Down Expand Up @@ -596,10 +615,12 @@ function action_acm_tag( $tag_id ) {
$display_codes = array();
foreach( (array)$this->ad_codes[$tag_id] as $ad_code ) {

// If the ad code doesn't have any conditionals,
// If the ad code doesn't have any conditionals and logical_operator set to "AND",
// we should add it to the display list
if ( empty( $ad_code['conditionals'] ) ) {
$display_codes[] = $ad_code;
if ( $this->logical_operator == 'AND' ) {
$display_codes[] = $ad_code;
}
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion common/js/acm.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jQuery( document ).ready( function( $ ) {
subgrid_lastsel=id;
}},
rowNum:5,
rowList:[5,10,20],
rowList:[5,10,20,50,100],
pager: '#acm-codes-conditionals-pager',
sortname: 'item',
jsonReader : { repeatitems: false }, // workaround for jqGrid issue
Expand Down
13 changes: 11 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ Contributors: danielbachhuber, rinatkhaziev, automattic
Tags: advertising, ad codes
Requires at least: 3.1
Tested up to: 3.3.1
Stable tag: 0.1
Stable tag: 0.1.1

Easy ad code management.
Easy ad code management.

== Description ==

Easily manage your ads via nice and simple UI. Each ad code has unlimited amount of conditionals. Conditionals are core WordPress methods like is_page(), is_category(), or your custom methods.

Currently works with:

* Doubleclick for publishers
* More to come

0 comments on commit 41cc15f

Please sign in to comment.