Skip to content

Instantly share code, notes, and snippets.

@anderly
Last active Sep 17, 2016
Embed
What would you like to do?
Ideas for making ShowDown extensible

Ideas for making ShowDown extensible:

I recently had to customize your plugin a bit for a client but wanted to suggest a few hooks and filters that could be added to the plugin to create some extensibility points for your customers.

Here are the hooks I added which would be great if you could add them into the main plugin so I could still get updates without having to do diffs when the plugin gets updated.

1st set of changes: Adding hooks to Anti-Cheat settings

File: wpshowdown.php

In the printAntiCheat() function, right after line that reads $wpShowdownOptions = $this->getOptions();, I added:

    $wpShowdownOptions = apply_filters( 'wpshowdown_anticheat_options', $wpShowdownOptions );

Next, inside the if block that saves the anticheat settings, after the line that reads $wpShowdownOptions = apply_filters( 'wpshowdown_save_anticheat_options', $_POST, $wpShowdownOptions );, I added:

    // Allow hooking to modify $wpShowdownOptions with additional settings
    $wpShowdownOptions = apply_filters( 'wpshowdown_save_anticheat_options', $_POST, $wpShowdownOptions );

    // Allow hooking when saving anticheat settings
    do_action( 'wpshowdown_saving_anticheat_options', $_POST, $wpShowdownOptions );

Next, before the submit button, I added:

    <?php
        // Allow hooking in to add additional anticheat settings
        do_action( 'wpshowdown_after_anticheat_settings', $wpShowdownOptions );
      ?>

2nd set of changes: Adding hooks to Showdown Engine

File: showdown_engine.php

First, inside shortcode_stats function, replaced switch statement with:

    // allow hooking it to add additional report types or modify existing ones
    $report_types = apply_filters( 'wpshowdownstats_report_types', array(
            // stats on top winners
            'top_winner' => array(
              'post_type' => 'competitor',
              'orderby' => 'meta_value_num',
              'meta_key' => 'won',
              'groups' => $group,
              'tag' => $tag,
              'posts_per_page' => $number 
            ),
            'top_loser' => array(
              'post_type' => 'competitor',
              'orderby' => 'meta_value_num',
              'meta_key' => 'lost',
              'groups' => $group,
              'tag' => $tag,
              'posts_per_page' => $number
            ),
            'top_drawn' => array(
              'post_type' => 'competitor',
              'orderby' => 'meta_value_num',
              'meta_key' => 'drawn',
              'groups' => $group,
              'tag' => $tag,
              'posts_per_page' => $number
            )
          ), $group, $tag, $number );

          $stats_query_args = $report_types[ $reporttype ];

          $loop = new WP_Query( $stats_query_args );

Next, inside shortcode_showdown function, right after the line that reads $result = "Indeterminate result";, I added:

    // Allow hooking in before results get processed
    do_action( 'wpshowdown_before_results_processed', $winner, $competitor1, $competitor2, $arenaOptions );

Finally, after the switch ($winner) statement, I added:

    // Allow hooking in after results get processed
    do_action( 'wpshowdown_after_results_processed', $winner, $competitor1, $competitor2, $arenaOptions );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment