Skip to content

Commit ccc9870

Browse files
committed
Markdownize contextual help. Now we don't need to write HTML contextual help, we parse markdown from the readme instead! Closes #28
1 parent 11b3618 commit ccc9870

File tree

3 files changed

+2910
-348
lines changed

3 files changed

+2910
-348
lines changed

ad-code-manager.php

Lines changed: 27 additions & 253 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
require_once( AD_CODE_MANAGER_ROOT .'/common/lib/acm-provider.php' );
3434
require_once( AD_CODE_MANAGER_ROOT .'/common/lib/acm-wp-list-table.php' );
3535
require_once( AD_CODE_MANAGER_ROOT .'/common/lib/acm-widget.php' );
36+
require_once( AD_CODE_MANAGER_ROOT .'/common/lib/markdown.php' );
3637

3738
class Ad_Code_Manager
3839
{
@@ -565,12 +566,29 @@ function action_load_ad_code_manager() {
565566
function admin_view_controller() {
566567
require_once( AD_CODE_MANAGER_ROOT . '/common/views/ad-code-manager.tpl.php' );
567568
}
569+
570+
function parse_readme_into_contextual_help() {
571+
ob_start();
572+
include_once(AD_CODE_MANAGER_ROOT . '/readme.txt' );
573+
$readme = ob_get_clean();
574+
$sections = preg_split( "/==(.*)==/", $readme );
575+
// Something's wrong with readme, fail silently
576+
if ( 5 > count( $sections) )
577+
return;
578+
579+
$useful = array( $sections[3], $sections[2], $sections[4] );
580+
foreach ( $useful as $i => $tab ) {
581+
// Because WP.ORG Markdown has a different flavor
582+
$useful[$i] = Markdown( str_replace(array('= ', ' ='), '**', $tab ) );
583+
}
584+
return $useful;
585+
}
568586

569587
function contextual_help() {
570588
global $pagenow;
571589
if ( 'tools.php' != $pagenow || !isset( $_GET['page'] ) || $_GET['page'] != $this->plugin_slug )
572590
return;
573-
591+
list( $installation, $description, $configuration ) = $this->parse_readme_into_contextual_help();
574592
ob_start();
575593
?>
576594
<div id="conditionals-help">
@@ -597,267 +615,23 @@ function contextual_help() {
597615
</div>
598616
<?php
599617
$contextual_help = ob_get_clean();
600-
601-
ob_start();
602-
?>
603-
<p>Ad Code Manager gives non-developers an interface in the WordPress admin for configuring your complex set of ad codes.</p>
604-
<p>We tried to streamline the process and make everyday AdOps a little bit easier</p>
605-
<p>Depending on ad network you use, you will see a set of required fields to fill in (generally, "Ad Code" is a set of parameters you need to pass to ad server, so it could serve proper ad). Then you set conditionals. You can create ad code with conditionals in one easy step</p>
606-
<p>Priorities work pretty much the same way they work in WordPress. Lower numbers correspond with higher priority.
607-
<p>Once you've done creating ad codes, you can easily implement them in your theme using:</p>
608-
<ul>
609-
<li>template tag: <code>&lt;?php do_action( 'acm_tag', $tag_id ) ?&gt;</code> </li>
610-
<li>shortcode: [acm-tag id="tag_id"]</li>
611-
<li>or using widget</li>
612-
</ul>
613-
614-
<?php
615-
$overview = ob_get_clean();
616-
ob_start();
617-
?>
618-
<p>There are some filters which will allow you to easily customize output of the plugin. You should place these filters in your themes functions.php file or someplace safe.</p>
619-
620-
<a href="https://gist.github.com/1631131" target="_blank">Check out this gist</a> to see all of the filters in action.
621-
622-
<p><strong>acm_default_url</strong></p>
623-
624-
<p>Currently, we don't store tokenized script URL anywhere so this filter is a nice place to set default value.</p>
625-
626-
<p>Arguments: <br />
627-
* string $url The tokenized url of Ad Code</p>
628-
<p>Example usage: Set your default ad code URL</p>
629-
<pre>
630-
add_filter( 'acm_default_url', 'my_acm_default_url' );
631-
function my_acm_default_url( $url ) {
632-
if ( 0 === strlen( $url ) ) {
633-
return "http://ad.doubleclick.net/adj/%site_name%/%zone1%;s1=%zone1%;s2=;pid=%permalink%;fold=%fold%;kw=;test=%test%;ltv=ad;pos=%pos%;dcopt=%dcopt%;tile=%tile%;sz=%sz%;";
634-
}
635-
}
636-
</pre>
637-
638-
<p><strong>acm_output_tokens</strong></p>
639-
640-
<p>Register output tokens depending on the needs of your setup. Tokens are the keys to be replaced in your script URL.</p>
641-
642-
<p>Arguments: <br/>
643-
* array $output_tokens Any existing output tokens <br/>
644-
* string $tag_id Unique tag id <br/>
645-
* array $code_to_display Ad Code that matched conditionals
646-
</p>
647-
<p>Example usage: Test to determine whether you're in test or production by passing ?test=on query argument</p>
648-
649-
<pre>
650-
add_filter( 'acm_output_tokens', 'my_acm_output_tokens', 10, 3 );
651-
function my_acm_output_tokens( $output_tokens, $tag_id, $code_to_display ) {
652-
$output_tokens['%test%'] = isset( $_GET['test'] ) && $_GET['test'] == 'on' ? 'on' : '';
653-
return $output_tokens;
654-
}`
655-
</pre>
656-
657-
<p><strong>acm_ad_tag_ids</strong></p>
658-
659-
<p>Extend set of default tag ids. Ad tag ids are used as a parameter for your template tag (e.g. do_action( 'acm_tag', 'my_top_leaderboard' ))</p>
660-
<p>Arguments: <br />
661-
* array $tag_ids array of default tag ids</p>
662-
663-
<p>Example usage: Add a new ad tag called 'my_top_leaderboard'</p>
664-
665-
<pre>
666-
add_filter( 'acm_ad_tag_ids', 'my_acm_ad_tag_ids' );
667-
function my_acm_ad_tag_ids( $tag_ids ) {
668-
$tag_ids[] = array(
669-
'tag' => 'my_top_leaderboard', // tag_id
670-
'url_vars' => array(
671-
'sz' => '728x90', // %sz% token
672-
'fold' => 'atf', // %fold% token
673-
'my_custom_token' => 'something' // %my_custom_token% will be replaced with 'something'
674-
);
675-
return $tag_ids;
676-
}
677-
</pre>
678-
679-
<p><strong>acm_output_html</strong></p>
680-
681-
<p>Support multiple ad formats ( e.g. Javascript ad tags, or simple HTML tags ) by adjusting the HTML rendered for a given ad tag.</p>
682-
683-
<p>Arguments: <br />
684-
* string $output_html The original output HTML <br />
685-
* string $tag_id Ad tag currently being accessed <br />
686-
</p>
687-
<p>Example usage:</p>
688-
<pre>
689-
add_filter( 'acm_output_html', 'my_acm_output_html', 10, 2 );
690-
function my_acm_output_html( $output_html, $tag_id ) {
691-
switch ( $tag_id ) {
692-
case 'my_leaderboard':
693-
$output_html = '&lt;a href="%url%"&gt; &lt;img src="%image_url%" /&gt;&lt;/a&gt;';
694-
break;
695-
case 'rich_media_leaderboard':
696-
$output_html = '&lt;script&gt; // omitted &lt;/script&gt;';
697-
break;
698-
default:
699-
break;
700-
}
701-
return $output_html;
702-
}
703-
</pre>
704-
<p><strong>acm_whitelisted_conditionals</strong></p>
705-
706-
<p>Extend the list of usable conditional functions with your own awesome ones. We whitelist these so users can't execute random PHP functions.</p>
707-
708-
<p>Arguments: <br />
709-
* array $conditionals Default conditionals</p>
710-
711-
<p>Example usage: Register a few custom conditional callbacks</p>
712-
713-
<pre>
714-
add_filter( 'acm_whitelisted_conditionals', 'my_acm_whitelisted_conditionals' );
715-
function my_acm_whitelisted_conditionals( $conditionals ) {
716-
$conditionals[] = 'my_is_post_type';
717-
$conditionals[] = 'is_post_type_archive';
718-
$conditionals[] = 'my_page_is_child_of';
719-
return $conditionals;
720-
}
721-
</pre>
722-
723-
<p><strong>acm_conditional_args</strong></p>
724-
725-
<p>For certain conditionals (has_tag, has_category), you might need to pass additional arguments.</p>
726-
727-
<p>Arguments: <br />
728-
* array $cond_args Existing conditional arguments <br />
729-
* string $cond_func Conditional function (is_category, is_page, etc)
730-
</p>
731-
732-
<p>Example usage: has_category() and has_tag() use has_term(), which requires the object ID to function properly</p>
733-
734-
<pre>
735-
add_filter( 'acm_conditional_args', 'my_acm_conditional_args', 10, 2 );
736-
function my_acm_conditional_args( $cond_args, $cond_func ) {
737-
global $wp_query;
738-
// has_category and has_tag use has_term
739-
// we should pass queried object id for it to produce correct result
740-
if ( in_array( $cond_func, array( 'has_category', 'has_tag' ) ) ) {
741-
if ( $wp_query->is_single == true ) {
742-
$cond_args[] = $wp_query->queried_object->ID;
743-
}
744-
}
745-
// my_page_is_child_of is our custom WP conditional tag and we have to pass queried object ID to it
746-
if ( in_array( $cond_func, array( 'my_page_is_child_of' ) ) && $wp_query->is_page ) {
747-
$cond_args[] = $cond_args[] = $wp_query->queried_object->ID;
748-
}
749-
750-
return $cond_args;
751-
}
752-
</pre>
753-
754-
<p><strong>acm_whitelisted_script_urls</strong></p>
755-
756-
<p>A security filter to whitelist which ad code script URLs can be added in the admin</p>
757618

758-
<p>Arguments: <br />
759-
* array $whitelisted_urls Existing whitelisted ad code URLs</p>
760-
761-
<p>Example usage: Allow Doubleclick for Publishers ad codes to be used</p>
762-
763-
<pre>add_filter( 'acm_whitelisted_script_urls', 'my_acm_whitelisted_script_urls' );
764-
function my_acm_whiltelisted_script_urls( $whitelisted_urls ) {
765-
$whitelisted_urls = array( 'ad.doubleclick.net' );
766-
return $whitelisted_urls;
767-
}
768-
</pre>
769-
770-
<p><strong>acm_display_ad_codes_without_conditionals</strong></p>
771-
772-
<p>Change the behavior of Ad Code Manager so that ad codes without conditionals display on the frontend. The default behavior is that each ad code requires a conditional to be included in the presentation logic.</p>
773-
774-
<p>Arguments: <br />
775-
* bool $behavior Whether or not to display the ad codes that don't have conditionals</p>
776-
777-
<p>Example usage:</p>
778-
779-
<pre>add_filter( 'acm_display_ad_codes_without_conditionals', '__return_true' );</pre>
780-
781-
<p><strong>acm_provider_slug</strong></p>
782-
783-
<p>By default we use our bundled doubleclick_for_publishers config ( check it in /providers/doubleclick-for-publishers.php ). If you want to add your own flavor of DFP or even implement configuration for some another ad network, you'd have to apply a filter to correct the slug.</p>
784-
785-
<p>Example usage:</p>
786-
787-
<pre>add_filter( 'acm_provider_slug', function() { return 'my-ad-network-slug'; })</pre>
788-
789-
<p><strong>acm_logical_operator</strong></p>
790-
791-
<p>By default logical operator is set to "OR", that is, ad code will be displayed if at least one conditional returns true.
792-
You can change it to "AND", so that ad code will be displayed only if ALL of the conditionals match</p>
793-
794-
<p>Example usage:</p>
795-
796-
<pre>add_filter( 'acm_provider_slug', function( $slug ) { return 'my-ad-network-slug'; })</pre>
797-
798-
<p><strong>acm_manage_ads_cap</strong></p>
799-
800-
<p>By default user has to have "manage_options" cap. This filter comes in handy, if you want to relax the requirements.</p>
801-
802-
<p>Example usage:</p>
803-
804-
<pre>add_filter( 'acm_manage_ads_cap', function( $cap ) { return 'edit_others_posts'; })</pre>
805-
806-
<p><strong>acm_allowed_get_posts_args</strong></p>
807-
808-
<p>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.</p>
809-
810-
<p>Example usage:</p>
811-
812-
<code>add_filter( 'acm_allowed_get_posts_args', function( $args_array ) { return array( 'offset', 'exclude' ); })</code>
813-
814-
<p><strong>acm_ad_code_count</strong></p>
815-
816-
<p>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.</p>
817-
818-
<p>Example usage:</p>
819-
820-
<pre>add_filter( 'acm_ad_code_count', function( $total ) { return 100; })</pre>
821-
822-
<p><strong>acm_list_table_columns</strong></p>
823-
824-
<p>This filter can alter table columns that are displayed in ACM UI.</p>
825-
826-
<p>Example usage:</p>
827-
828-
<pre>add_filter( 'acm_list_table_columns', function ( $columns ) {
829-
$columns = array(
830-
'id' => __( 'ID', 'ad-code-manager' ),
831-
'name' => __( 'Name', 'ad-code-manager' ),
832-
'priority' => __( 'Priority', 'ad-code-manager' ),
833-
'conditionals' => __( 'Conditionals', 'ad-code-manager' ),
834-
);
835-
return $columns;
836-
} )
837-
</pre>
838-
<p><strong>acm_provider_columns</strong></p>
839-
840-
<p>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
841-
'id', 'name', 'priority', 'conditionals'. All of them except name are generic for Ad Code Manager. Hence acm_provider_columns should return only "name"</p>
842-
843-
<p>Example usage:</p>
844-
<pre>add_filter( 'acm_provider_columns', function ( $columns ) {
845-
$columns = array(
846-
'name' => __( 'Name', 'ad-code-manager' ),
847-
);
848-
return $columns;
849-
} )</pre>
850-
<?php
851-
$configuration = ob_get_clean();
852619

853620

854621
get_current_screen()->add_help_tab(
855622
array(
856623
'id' => 'acm-overview',
857624
'title' => 'Overview',
858-
'content' => $overview,
625+
'content' => $description,
859626
)
860627
);
628+
get_current_screen()->add_help_tab(
629+
array(
630+
'id' => 'acm-install',
631+
'title' => 'Installation',
632+
'content' => $installation,
633+
)
634+
);
861635
get_current_screen()->add_help_tab(
862636
array(
863637
'id' => 'acm-config',

0 commit comments

Comments
 (0)