| @@ -1,0 +1,64 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Achievement Widget | |
| 4 | + * | |
| 5 | + * @package GamiPress\Widgets\Widget\Achievement | |
| 6 | + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +class GamiPress_Achievement_Widget extends GamiPress_Widget { | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * Shortcode for this widget. | |
| 16 | + * | |
| 17 | + * @var string | |
| 18 | + */ | |
| 19 | + protected $shortcode = 'gamipress_achievement'; | |
| 20 | + | |
| 21 | + public function __construct() { | |
| 22 | + | |
| 23 | + parent::__construct( | |
| 24 | + $this->shortcode . '_widget', | |
| 25 | + __( 'GamiPress: Achievement', 'gamipress' ), | |
| 26 | + __( 'Display a desired achievement.', 'gamipress' ) | |
| 27 | + ); | |
| 28 | + | |
| 29 | + } | |
| 30 | + | |
| 31 | + public function get_fields() { | |
| 32 | + | |
| 33 | + // Need to change field title to show_title to avoid problems with widget title field | |
| 34 | + $fields = GamiPress()->shortcodes[$this->shortcode]->fields; | |
| 35 | + | |
| 36 | + // Get the fields keys | |
| 37 | + $keys = array_keys( $fields ); | |
| 38 | + | |
| 39 | + // Get the numeric index of the field 'title' | |
| 40 | + $index = array_search( 'title', $keys ); | |
| 41 | + | |
| 42 | + // Replace the 'title' key by 'show_title' | |
| 43 | + $keys[$index] = 'show_title'; | |
| 44 | + | |
| 45 | + // Combine new array with new keys with an array of values | |
| 46 | + $fields = array_combine( $keys, array_values( $fields ) ); | |
| 47 | + | |
| 48 | + return $fields; | |
| 49 | + | |
| 50 | + } | |
| 51 | + | |
| 52 | + public function get_widget( $args, $instance ) { | |
| 53 | + | |
| 54 | + // Get back replaced fields | |
| 55 | + $instance['title'] = $instance['show_title']; | |
| 56 | + | |
| 57 | + // Build shortcode attributes from widget instance | |
| 58 | + $atts = gamipress_build_shortcode_atts( $this->shortcode, $instance ); | |
| 59 | + | |
| 60 | + echo gamipress_do_shortcode( $this->shortcode, $atts ); | |
| 61 | + | |
| 62 | + } | |
| 63 | + | |
| 64 | +} | |