| 1 |
<?php |
| 2 |
/** |
| 3 |
* User Rank Widget |
| 4 |
* |
| 5 |
* @package GamiPress\Widgets\Widget\User_Rank |
| 6 |
* @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.3.1 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class GamiPress_User_Rank_Widget extends GamiPress_Widget { |
| 13 |
|
| 14 |
/** |
| 15 |
* Shortcode for this widget. |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
protected $shortcode = 'gamipress_user_rank'; |
| 20 |
|
| 21 |
public function __construct() { |
| 22 |
|
| 23 |
parent::__construct( |
| 24 |
$this->shortcode . '_widget', |
| 25 |
__( 'GamiPress: User Rank', 'gamipress' ), |
| 26 |
__( 'Display previous, current and/or next rank of a user.', 'gamipress' ) |
| 27 |
); |
| 28 |
|
| 29 |
} |
| 30 |
|
| 31 |
public function get_tabs() { |
| 32 |
|
| 33 |
$tabs = GamiPress()->shortcodes[$this->shortcode]->tabs; |
| 34 |
|
| 35 |
// Add the widget title field to the general tab |
| 36 |
$tabs['general']['fields'][] = 'title'; |
| 37 |
|
| 38 |
// Add the renamed rank title field to the achievement tab |
| 39 |
$tabs['rank']['fields'][] = 'show_title'; |
| 40 |
|
| 41 |
// Get the numeric index of the rank field 'title' |
| 42 |
$index = array_search( 'title', $tabs['rank']['fields'] ); |
| 43 |
|
| 44 |
// Remove the title from this tab |
| 45 |
unset( $tabs['rank']['fields'][$index] ); |
| 46 |
|
| 47 |
return $tabs; |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
public function get_fields() { |
| 52 |
|
| 53 |
// Need to change field title to show_title to avoid problems with widget title field |
| 54 |
$fields = GamiPress()->shortcodes[$this->shortcode]->fields; |
| 55 |
|
| 56 |
// Get the fields keys |
| 57 |
$keys = array_keys( $fields ); |
| 58 |
|
| 59 |
// Get the numeric index of the field 'title' |
| 60 |
$index = array_search( 'title', $keys ); |
| 61 |
|
| 62 |
// Replace the 'title' key by 'show_title' |
| 63 |
$keys[$index] = 'show_title'; |
| 64 |
|
| 65 |
// Combine new array with new keys with an array of values |
| 66 |
$fields = array_combine( $keys, array_values( $fields ) ); |
| 67 |
|
| 68 |
return $fields; |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
public function get_widget( $args, $instance ) { |
| 73 |
|
| 74 |
// Get back replaced fields |
| 75 |
$instance['title'] = $instance['show_title']; |
| 76 |
|
| 77 |
// Build shortcode attributes from widget instance |
| 78 |
$atts = gamipress_build_shortcode_atts( $this->shortcode, $instance ); |
| 79 |
|
| 80 |
echo gamipress_do_shortcode( $this->shortcode, $atts ); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
} |