Created
February 21, 2013 08:31
-
-
Save mattyza/40407d54ee00660413ba to your computer and use it in GitHub Desktop.
WooSlider Slideshows API example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: WooSlider - My Custom Slideshow | |
Plugin URI: http://your-plugin-website.com/ | |
Description: This is my custom WooSlider slideshow. | |
Version: 1.0.0 | |
Author: Your Name Here | |
Author URI: http://your-website.com/ | |
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html | |
*/ | |
add_action( 'plugins_loaded', array( 'My_Custom_Slideshow', 'init' ), 0 ); | |
class My_Custom_Slideshow { | |
/** | |
* Initialize the plugin, check the environment and make sure we can act. | |
* @since 1.0.0 | |
* @return void | |
*/ | |
public function init () { | |
// Make sure both WooSlider and WooCommerce are active. | |
$active_plugins = apply_filters( 'active_plugins', get_option('active_plugins' ) ); | |
if ( ! in_array( 'wooslider/wooslider.php', $active_plugins ) ) return; | |
// Add the slideshow type into WooSlider. | |
add_filter( 'wooslider_slider_types', array( 'My_Custom_Slideshow', 'add_slideshow_type' ) ); | |
// Add support for the type-specific fields when generating the WooSlider shortcode. | |
add_filter( 'wooslider_popup_settings', array( 'My_Custom_Slideshow', 'add_popup_fields' ) ); | |
// Add the slideshow type's fields into the WooSlider popup. | |
add_action( 'wooslider_popup_conditional_fields', array( 'My_Custom_Slideshow', 'display_fields' ) ); | |
} // End init() | |
/** | |
* Integrate the slideshow type into WooSlider. | |
* @since 1.0.0 | |
* @param array $types Existing slideshow types. | |
* @return array $types Modified array of types. | |
*/ | |
public function add_slideshow_type ( $types ) { | |
if ( is_array( $types ) ) { | |
// Make sure to add an array, at our desired key, consisting of a "name" and the "callback" function to get the slides for this slideshow type. | |
$types['my-custom-slideshow'] = array( 'name' => __( 'My Custom Slideshow Type', 'my-custom-slideshow' ), 'callback' => array( 'My_Custom_Slideshow', 'get_slides' ) ); | |
} | |
return $types; | |
} // End add_slideshow_type() | |
/** | |
* Add support for our slideshow-specific fields when generating the shortcode. | |
* @since 1.0.0 | |
* @param array $fields Array of supported settings. | |
* @return array Modified supported settings array. | |
*/ | |
public function add_popup_fields ( $fields ) { | |
if ( is_array( $fields ) ) { | |
// Add a new key to the array of supported fields when generating the shortcode, with the value being set to the default value or an empty string. | |
$fields['display_as_titles'] = ''; | |
} | |
return $fields; | |
} // End add_popup_fields() | |
/** | |
* Display conditional fields for this slideshow type, when generating the shortcode. | |
* @since 1.0.0 | |
* @return void | |
*/ | |
public function display_fields () { | |
global $wooslider; | |
// Get an array of the fields, and their settings, to be generated in the popup form for conditional fields for this slideshow type. | |
$fields = self::get_fields(); | |
// Make sure that the DIV tag below has a CSS class of "conditional-slideshowtype", where "slideshowtype" is our newly added type. | |
?> | |
<div class="conditional conditional-my-custom-slideshow"> | |
<table class="form-table"> | |
<tbody> | |
<?php foreach ( $fields as $k => $v ) { ?> | |
<tr valign="top"> | |
<th scope="row"><?php echo $v['name']; ?></th> | |
<td> | |
<?php | |
// Use WooSlider's admin object to generate the desired field according to it's type. | |
$wooslider->admin->generate_field_by_type( $v['type'], $v['args'] ); | |
?> | |
<?php if ( $v['description'] != '' ) { ?><p><span class="description"><?php echo $v['description']; ?></span></p><?php } ?> | |
</td> | |
</tr> | |
<?php } ?> | |
</tbody> | |
</table> | |
</div><!--/.conditional--> | |
<?php | |
} // End display_fields() | |
/** | |
* Generate an array of the data to be used to generate the fields for display in the WooSlider admin. | |
* @since 1.0.0 | |
* @return array Field data. | |
*/ | |
private function get_fields () { | |
$fields = array(); | |
$display_as_titles_args = array( 'key' => 'display_as_titles', 'data' => array() ); | |
$fields['display_as_titles'] = array( 'name' => __( 'Display text as titles', 'my-custom-slideshow' ), 'type' => 'checkbox', 'args' => $display_as_titles_args, 'description' => __( 'Display text as titles', 'my-custom-slideshow' ) ); | |
return $fields; | |
} // End get_fields() | |
/** | |
* Get the slides for this slideshow type. | |
* @since 1.0.0 | |
* @param array $args Arguments from the shortcode. | |
* @return array An array of slides to display in the slideshow. | |
*/ | |
public function get_slides ( $args = array() ) { | |
$slides = array(); | |
// Setup default arguments for this slideshow type. | |
$defaults = array( | |
'display_as_titles' => 'false' | |
); | |
$args = wp_parse_args( $args, $defaults ); | |
$slides[] = array( 'content' => 'This is slide 01.' ); | |
$slides[] = array( 'content' => 'This is slide 02.' ); | |
$slides[] = array( 'content' => 'This is slide 03.' ); | |
$slides[] = array( 'content' => 'This is slide 04.' ); | |
// A bit of custom code to convert our text to headings, based on the arguments. | |
if ( 'true' == $args['display_titles'] ) { | |
foreach ( $slides as $k => $v ) { | |
$slides[$k]['content'] = '<h3>' . $v . '</h3>'; | |
} | |
} | |
return $slides; | |
} // End get_slides() | |
} // End Class | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment