Skip to content

Commit 377a7aa

Browse files
Merge branch 'develop' of github.com:Automattic/Ad-Code-Manager
2 parents 10c638d + e53e198 commit 377a7aa

File tree

7 files changed

+88
-53
lines changed

7 files changed

+88
-53
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "tests"]
2+
path = tests
3+
url = git://github.com/rinatkhaziev/acm-tests.git

ad-code-manager.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,19 @@ function handle_admin_action() {
228228
$ad_code_vals = array(
229229
'priority' => $priority,
230230
);
231-
foreach( $this->current_provider->columns as $slug => $title ) {
232-
$ad_code_vals[$slug] = sanitize_text_field( $_REQUEST['acm-column'][$slug] );
231+
foreach( $this->current_provider->ad_code_args as $arg ) {
232+
$ad_code_vals[$arg['key']] = sanitize_text_field( $_REQUEST['acm-column'][$arg['key']] );
233233
}
234234
if ( $_REQUEST['method'] == 'add')
235235
$id = $this->create_ad_code( $ad_code_vals );
236236
else
237237
$id = $this->edit_ad_code( $id, $ad_code_vals );
238238
if ( is_wp_error( $id ) ) {
239-
$message = 'error-adding-editing-ad-code';
239+
// We can die with an error if this is an edit/ajax request
240+
if ( isset( $id->errors['edit-error'][0] ) )
241+
die( '<div class="error">' . $id->errors['edit-error'][0] . '</div>' );
242+
else
243+
$message = 'error-adding-editing-ad-code';
240244
break;
241245
}
242246
$new_conditionals = array();
@@ -323,8 +327,8 @@ function get_ad_codes( $query_args = array() ) {
323327
foreach ( $ad_codes as $ad_code_cpt ) {
324328
$provider_url_vars = array();
325329

326-
foreach ( $this->current_provider->columns as $slug => $title ) {
327-
$provider_url_vars[$slug] = get_post_meta( $ad_code_cpt->ID, $slug, true );
330+
foreach ( $this->current_provider->ad_code_args as $arg ) {
331+
$provider_url_vars[$arg['key']] = get_post_meta( $ad_code_cpt->ID, $arg['key'], true );
328332
}
329333

330334
$priority = get_post_meta( $ad_code_cpt->ID, 'priority', true );
@@ -355,8 +359,8 @@ function get_ad_code( $post_id ) {
355359
return false;
356360

357361
$provider_url_vars = array();
358-
foreach ( $this->current_provider->columns as $slug => $title ) {
359-
$provider_url_vars[$slug] = get_post_meta( $post->ID, $slug, true );
362+
foreach ( $this->current_provider->ad_code_args as $arg ) {
363+
$provider_url_vars[$arg['key']] = get_post_meta( $post->ID, $arg['key'], true );
360364
}
361365

362366
$priority = get_post_meta( $post_id, 'priority', true );
@@ -401,13 +405,13 @@ function get_conditionals( $ad_code_id ) {
401405
*/
402406
function create_ad_code( $ad_code = array() ) {
403407
$titles = array();
404-
foreach ( $this->current_provider->columns as $slug => $col_title ) {
408+
foreach ( $this->current_provider->ad_code_args as $arg ) {
405409
// We shouldn't create an ad code,
406410
// If any of required fields is not set
407-
if ( ! $ad_code[$slug] ) {
408-
return;
411+
if ( ! isset( $ad_code[$arg['key']] ) && $arg['required'] === true ) {
412+
return new WP_Error();
409413
}
410-
$titles[] = $ad_code[$slug];
414+
$titles[] = $ad_code[$arg['key']];
411415
}
412416
$acm_post = array(
413417
'post_title' => implode( '-', $titles ),
@@ -418,8 +422,8 @@ function create_ad_code( $ad_code = array() ) {
418422
);
419423

420424
if ( ! is_wp_error( $acm_inserted_post_id = wp_insert_post( $acm_post, true ) ) ) {
421-
foreach ( $this->current_provider->columns as $slug => $title ) {
422-
update_post_meta( $acm_inserted_post_id, $slug, $ad_code[$slug] );
425+
foreach ( $this->current_provider->ad_code_args as $arg ) {
426+
update_post_meta( $acm_inserted_post_id, $arg['key'], $ad_code[$arg['key']] );
423427
}
424428
update_post_meta( $acm_inserted_post_id, 'priority', $ad_code['priority'] );
425429
$this->flush_cache();
@@ -432,16 +436,15 @@ function create_ad_code( $ad_code = array() ) {
432436
* Update an existing ad code
433437
*/
434438
function edit_ad_code( $ad_code_id, $ad_code = array()) {
435-
foreach ( $this->current_provider->columns as $slug => $title ) {
436-
// We shouldn't update an ad code,
437-
// If any of required fields is not set
438-
if ( ! $ad_code[$slug] ) {
439-
return new WP_Error();
439+
foreach ( $this->current_provider->ad_code_args as $arg ) {
440+
// If a required argument is not set, we return an error message with the missing parameter
441+
if ( ! isset( $ad_code[$arg['key']] ) && $arg['required'] === true ) {
442+
return new WP_Error( 'edit-error', 'Error updating ad code, a parameter for ' . esc_html( $arg['key'] ) . ' is required.' );
440443
}
441444
}
442445
if ( 0 !== $ad_code_id ) {
443-
foreach ( $this->current_provider->columns as $slug => $title ) {
444-
update_post_meta( $ad_code_id, $slug, $ad_code[$slug] );
446+
foreach ( $this->current_provider->ad_code_args as $arg ) {
447+
update_post_meta( $ad_code_id, $arg['key'], $ad_code[$arg['key']] );
445448
}
446449
update_post_meta( $ad_code_id, 'priority', $ad_code['priority'] );
447450
}

common/lib/acm-provider.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @property string $output_html Html of an ad tag
99
* @property array $output_tokens Array of tokens that will be replaced in %url%
1010
* @property array $ad_tag_ids Set of default ad tags (e.g. 2 leaderboards, 300x250, etc)
11-
* @property array $columns array of properties of an ad code in format "slug" => 'Column title'
11+
* @property array $ad_code_args array of properties of an ad code
1212
*
1313
* @since v0.1.3
1414
*/
@@ -18,13 +18,19 @@ class ACM_Provider
1818
public $output_html;
1919
public $output_tokens = array();
2020
public $ad_tag_ids;
21-
public $columns = array();
22-
21+
public $ad_code_args = array();
2322
function __construct() {
24-
if ( empty( $this->columns ) ) {
23+
if ( empty( $this->ad_code_args ) ) {
2524
// This is not actual data, but rather format:
26-
// slug => Title
27-
$this->columns = array('name' => 'Name');
25+
$this->ad_code_args = array(
26+
array(
27+
'key' => 'name',
28+
'label' => __( 'Name', 'ad-code-manager' ),
29+
'editable' => true,
30+
'required' => true,
31+
),
32+
);
33+
$this->ad_code_args = apply_filters( 'acm_ad_code_args', $this->ad_code_args );
2834
}
2935

3036
// Could be filtered via acm_output_html filter

common/views/ad-code-manager.tpl.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,15 @@
5555
<?php wp_nonce_field( 'acm-admin-action', 'nonce' ); ?>
5656

5757
<?php
58-
foreach ( $this->current_provider->columns as $slug => $title ):
59-
$column_id = 'acm-column[' . $slug . ']';
58+
foreach ( $this->current_provider->ad_code_args as $arg ):
59+
if ( ! $arg['editable'] )
60+
continue;
61+
62+
$column_id = 'acm-column[' . $arg['key'] . ']';
6063
?>
6164
<div class="form-field form-required">
62-
<label for="<?php echo esc_attr( $column_id ) ?>"><?php echo esc_html( $title ) ?></label>
63-
<input name="<?php echo esc_attr( $column_id ) ?>" id="<?php echo esc_attr( $column_id ) ?>" type="text" value="" size="40" aria-required="true">
65+
<label for="<?php echo esc_attr( $column_id ) ?>"><?php echo esc_html( $arg['label'] ) ?></label>
66+
<input name="<?php echo esc_attr( $column_id ) ?>" id="<?php echo esc_attr( $column_id ) ?>" type="text" value="" size="40" aria-required="<?php echo $arg['editable'] ?>">
6467
</div>
6568
<?php
6669
endforeach;

providers/doubleclick-for-publishers.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
*
55
* @since 0.1.3
66
*/
7+
class Doubleclick_For_Publishers_Columns {
8+
9+
}
10+
711
class Doubleclick_For_Publishers_ACM_Provider extends ACM_Provider {
812
function __construct() {
913
// Default output HTML
@@ -57,11 +61,23 @@ function __construct() {
5761
)
5862
),
5963
);
60-
64+
$this->ad_code_args = array(
65+
array(
66+
'key' => 'site_name',
67+
'label' => __( 'Site Name', 'ad-code-manager' ),
68+
'editable' => true,
69+
'required' => true,
70+
),
71+
array(
72+
'key' => 'zone1',
73+
'label' => __( 'zone1', 'ad-code-manager' ),
74+
'editable' => true,
75+
'required' => true,
76+
),
77+
);
6178
// Only allow ad tags called from following URLS
6279
$this->whitelisted_script_urls = array( 'ad.doubleclick.net' );
63-
$this->columns = apply_filters( 'acm_provider_columns', array( 'site_name' => 'Site Name', 'zone1' => 'zone1' ) );
64-
80+
6581
parent::__construct();
6682
}
6783

readme.txt

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ By default we use our bundled doubleclick_for_publishers config ( check it in /p
216216

217217
Example usage:
218218

219-
`add_filter( 'acm_provider_slug', function() { return 'my-ad-network-slug'; })`
219+
`add_filter( 'acm_provider_slug', function() { return 'my-ad-network-slug'; } );`
220220

221221
= acm_logical_operator =
222222

@@ -225,60 +225,63 @@ You can change it to "AND", so that ad code will be displayed only if ALL of the
225225

226226
Example usage:
227227

228-
`add_filter( 'acm_provider_slug', function( $slug ) { return 'my-ad-network-slug'; })`
228+
`add_filter( 'acm_logical_operator', function() { return 'AND'; } );`
229229

230230
= acm_manage_ads_cap =
231231

232232
By default user has to have "manage_options" cap. This filter comes in handy, if you want to relax the requirements.
233233

234234
Example usage:
235235

236-
`add_filter( 'acm_manage_ads_cap', function( $cap ) { return 'edit_others_posts'; })`
236+
`add_filter( 'acm_manage_ads_cap', function( $cap ) { return 'edit_others_posts'; } );`
237237

238238
= acm_allowed_get_posts_args =
239239

240240
This filter is only for edge cases. Most likely you won't have to touch it. Allows to include additional query args for Ad_Code_Manager->get_ad_codes() method.
241241

242242
Example usage:
243243

244-
`add_filter( 'acm_allowed_get_posts_args', function( $args_array ) { return array( 'offset', 'exclude' ); })`
244+
`add_filter( 'acm_allowed_get_posts_args', function( $args_array ) { return array( 'offset', 'exclude' ); } );`
245245

246246
= acm_ad_code_count =
247247

248248
By default the total number of ad codes to get is 50, which is reasonable for any small to mid site. However, in some certain cases you would want to increase the limit. This will affect Ad_Code_Manager->get_ad_codes() 'numberposts' query argument.
249249

250250
Example usage:
251251

252-
`add_filter( 'acm_ad_code_count', function( $total ) { return 100; })`
252+
`add_filter( 'acm_ad_code_count', function( $total ) { return 100; } );`
253253

254254
= acm_list_table_columns =
255255

256256
This filter can alter table columns that are displayed in ACM UI.
257257

258258
Example usage:
259259

260-
`add_filter( 'acm_list_table_columns', function ( $columns ) {
261-
$columns = array(
262-
'id' => __( 'ID', 'ad-code-manager' ),
263-
'name' => __( 'Name', 'ad-code-manager' ),
264-
'priority' => __( 'Priority', 'ad-code-manager' ),
265-
'conditionals' => __( 'Conditionals', 'ad-code-manager' ),
266-
);
267-
return $columns;
268-
} )`
260+
`add_filter( 'acm_list_table_columns', 'my_acm_list_table_columns' );
261+
function my_acm_list_table_columns( $columns ) {
262+
$columns = array(
263+
'id' => __( 'ID', 'ad-code-manager' ),
264+
'name' => __( 'Name', 'ad-code-manager' ),
265+
'priority' => __( 'Priority', 'ad-code-manager' ),
266+
'conditionals' => __( 'Conditionals', 'ad-code-manager' ),
267+
);
268+
return $columns;
269+
}`
269270

270271
= acm_provider_columns =
271272

272273
This filter comes in pair with previous one, it should return array of ad network specific parameters. E.g. in acm_list_table_columns example we have
273274
'id', 'name', 'priority', 'conditionals'. All of them except name are generic for Ad Code Manager. Hence acm_provider_columns should return only "name"
274275

275276
Example usage:
276-
`add_filter( 'acm_provider_columns', function ( $columns ) {
277-
$columns = array(
278-
'name' => __( 'Name', 'ad-code-manager' ),
279-
);
280-
return $columns;
281-
} )`
277+
278+
`add_filter( 'acm_provider_columns', 'my_acm_provider_columns' );
279+
function my_acm_provider_columns( $columns ) {
280+
$columns = array(
281+
'name' => __( 'Name', 'ad-code-manager' ),
282+
);
283+
return $columns;
284+
}`
282285

283286
== Screenshots ==
284287

tests

Submodule tests added at 62aa5ea

0 commit comments

Comments
 (0)