| 1 |
<?php |
| 2 |
namespace MaxButtons; |
| 3 |
|
| 4 |
/** |
| 5 |
* Use of this field requires at least WordPress 3.5. |
| 6 |
* |
| 7 |
* Class SiteOrigin_Widget_Field_Media |
| 8 |
*/ |
| 9 |
class MaxButton_Widget_Field_MaxButton extends \SiteOrigin_Widget_Field_Base { |
| 10 |
/** |
| 11 |
* A label for the title of the media selector dialog. |
| 12 |
* |
| 13 |
* @access protected |
| 14 |
* @var string |
| 15 |
*/ |
| 16 |
protected $choose; |
| 17 |
/** |
| 18 |
* A label for the confirmation button of the media selector dialog. |
| 19 |
* |
| 20 |
* @access protected |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $update; |
| 24 |
/** |
| 25 |
* Sets the media library which to browse and from which media can be selected. Allowed values are 'image', |
| 26 |
* 'audio', 'video', and 'file'. The default is 'file'. |
| 27 |
* |
| 28 |
* @access protected |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
// protected $library; |
| 32 |
/** |
| 33 |
* Whether or not to display a URL input field which allows for specification of a fallback URL to be used in case |
| 34 |
* the selected media resource isn't available. |
| 35 |
* |
| 36 |
* @access protected |
| 37 |
* @var bool |
| 38 |
*/ |
| 39 |
protected $fallback; |
| 40 |
/** |
| 41 |
* Reference to the containing widget required for creating the fallback subfield. |
| 42 |
* |
| 43 |
* @access private |
| 44 |
* @var SiteOrigin_Widget |
| 45 |
*/ |
| 46 |
protected $for_widget; |
| 47 |
/** |
| 48 |
* An array of field names of parent repeaters. |
| 49 |
* |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
private $parent_repeater; |
| 53 |
|
| 54 |
public function __construct( $base_name, $element_id, $element_name, $field_options, $for_widget, $parent_container = array() ) { |
| 55 |
parent::__construct( $base_name, $element_id, $element_name, $field_options ); |
| 56 |
|
| 57 |
$this->for_widget = $for_widget; |
| 58 |
$this->parent_repeater = $parent_container; |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
protected function get_default_options() { |
| 63 |
return array( |
| 64 |
'choose' => __( 'Choose Maxbutton', 'maxbuttons' ), |
| 65 |
'update' => __( 'Set Media', 'maxbuttons' ), |
| 66 |
// 'library' => 'image' |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
protected function render_field( $value, $instance ) { |
| 71 |
//$buttons = MB() |
| 72 |
?> |
| 73 |
<script language="javascript"> |
| 74 |
function insertSOPageBuilder(id) |
| 75 |
{ |
| 76 |
var button = jQuery('.media-buttons .maxbutton-' + id).parents('.shortcode-container').children().clone(); |
| 77 |
jQuery("#SOP_selected").html(button); |
| 78 |
jQuery('#sop_selected_button').val(id); |
| 79 |
return false; |
| 80 |
|
| 81 |
} |
| 82 |
</script> |
| 83 |
<button class="button-primary maxbutton_media_button" data-callback='insertSOPageBuilder'><?php _e("Select a Button"); ?></button> |
| 84 |
<p><?php _e('Selected Button', 'maxbuttons') ?></p> |
| 85 |
<div id='SOP_selected'><?php |
| 86 |
$button= MB()->getClass('button'); |
| 87 |
|
| 88 |
if (intval($value) > 0) |
| 89 |
{ |
| 90 |
$button->set($value); |
| 91 |
$button->display(array('load_css' => 'inline') ); |
| 92 |
} |
| 93 |
|
| 94 |
?> </div> |
| 95 |
|
| 96 |
<input type="hidden" id="sop_selected_button" value="<?php echo esc_attr( is_array( $value ) ? '-1' : $value ) ?>" name="<?php echo esc_attr( $this->element_name ) ?>" class="siteorigin-widget-input" /> |
| 97 |
|
| 98 |
<?php |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
// $instance was added somehow by siteorigin making the plugin crash - added null for backward compat. |
| 103 |
protected function sanitize_field_input( $value, $instance = null ) { |
| 104 |
// MB value should be integer - button_id |
| 105 |
return intval( $value ); |
| 106 |
} |
| 107 |
|
| 108 |
public function sanitize_instance( $instance ) { |
| 109 |
$fallback_name = $this->get_fallback_field_name( $this->base_name ); |
| 110 |
if( !empty( $this->fallback ) && !empty( $instance[ $fallback_name ] ) ) { |
| 111 |
$instance[ $fallback_name ] = esc_url_raw( $instance[ $fallback_name ] ); |
| 112 |
} |
| 113 |
return $instance; |
| 114 |
} |
| 115 |
|
| 116 |
public function get_fallback_field_name( $base_name ) { |
| 117 |
$v_name = $base_name; |
| 118 |
if( strpos($v_name, '][') !== false ) { |
| 119 |
// Remove this splitter |
| 120 |
$v_name = substr( $v_name, strpos($v_name, '][') + 2 ); |
| 121 |
} |
| 122 |
return $v_name . '_fallback'; |
| 123 |
} |
| 124 |
} |
| 125 |
|