Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The instance was missing #52

Merged
merged 1 commit into from Oct 28, 2016
Merged

Conversation

pablo-sg-pacheco
Copy link
Contributor

The widget's instance was missing on get_widget

The widget's instance was missing on get_widget
@jtsternberg jtsternberg merged commit 0adb7a4 into CMB2:master Oct 28, 2016
@jtsternberg
Copy link
Member

thanks!

@jtsternberg
Copy link
Member

Actually, i reverted this. The instance is taken care of here: https://github.com/WebDevStudios/CMB2-Snippet-Library/blob/master/widgets/widget-example.php#L174-L178

@pablo-sg-pacheco
Copy link
Contributor Author

You're welcome!
But there is still a problem. The $atts param of the function
"get_widget" is getting replaced by another variable with the same name. I
think that is the problem

2016-10-28 12:33 GMT-02:00 Justin Sternberg notifications@github.com:

Actually, i reverted this. The instance is taken care of here:
https://github.com/WebDevStudios/CMB2-Snippet-Library/blob/master/widgets/
widget-example.php#L174-L178


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#52 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AH0FfLapxzWtnBW-KP0I9QACw3Xf-0fDks5q4gejgaJpZM4KjfEH
.

jtsternberg added a commit that referenced this pull request Oct 28, 2016
@jtsternberg
Copy link
Member

What about with that change ^?

@pablo-sg-pacheco
Copy link
Contributor Author

I tested here and it still doesn't work.
This way we are getting only the default values that were set on
__construct.

I only could get what i wanted with this change:

'instance' => $atts['instance'],

2016-10-28 13:47 GMT-02:00 Justin Sternberg notifications@github.com:

What about with that change ^?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#52 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AH0FfOItanB1N3BuPd8Y0ztT6vTYATcXks5q4hj_gaJpZM4KjfEH
.

@jtsternberg
Copy link
Member

can you show me your code?

@pablo-sg-pacheco
Copy link
Contributor Author

pablo-sg-pacheco commented Oct 28, 2016

Do you want me to create another pull request?

My code is actually a little bit different and is not pretty :(. I'm using
cmb2 select2 also, but anyway here it goes:

widget_slug, esc_html**( 'Filtros de filmes', 'your-textdomain' ), array( 'classname' => $this->widget_slug, 'customize_selective_refresh' => true, 'description' => esc_html__( 'Filtra os filmes', 'your-textdomain' ), ) ); $taxesArr = array( 'movie_cast'=>'Elenco', 'movie_country'=>'País', 'movie_direction'=>'Direção', 'movie_genre'=>'Gênero', 'movie_script'=>'Roteiro', 'movie_year'=>'Ano de lançamento', 'movie_cat'=>'Categoria', 'movie_tag'=>'Tag', 'movie_mood'=>'Estado de espírito' ); /_foreach ( $taxesArr as $key => $value ) { $taxesArr[$key]=get_taxonomy($key)->labels->name; }*/ self::$defaults = array( 'title' => esc_html__( 'Filtro de filmes', 'your-textdomain' ), 'image' => '', self::META_TAXONOMIES => '' ); $this->cmb2_fields = array( array( 'name' => 'Título', 'id_key' => 'title', 'id' => 'title', 'type' => 'text', ), array( 'name' => esc_html__('Filtros', 'cmb2'), 'id' => self::META_TAXONOMIES, 'id_key' => self::META_TAXONOMIES, //'desc' => 'Select ingredients. Drag to reorder.', 'type' => 'pw_multiselect', 'options' => $taxesArr ), ); add_action( 'save_post', array( $this, 'flush_widget_cache' ) ); add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) ); add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) ); add_shortcode( self::$shortcode, array( **CLASS**, 'get_widget' ) ); } /** - Delete this widget's cache. * - Note: Could also delete any transients - delete_transient( 'some-transient-generated-by-this-widget' ); */ public function flush_widget_cache() { wp_cache_delete( $this->id, 'widget' ); } /** - Front-end display of widget. * - @param array $args The widget arguments set up when a sidebar is registered. - @param array $instance The widget settings as set by user. _/ public function widget( $args, $instance ) { echo self::get_widget( array( 'args' => $args, 'instance' => $instance, 'cache_id' => $this->id, // whatever the widget id is ) ); } /_* - Return the widget/shortcode output * - @param array $atts Array of widget/shortcode attributes/args - @return string Widget output */ public static function get_widget( $atts ) { $functions = \OOP_Functions\OOP_Functions::getInstance(); $taxes = $functions->getTaxonomies(); $widget = ''; // Set up default values for attributes $atts = shortcode_atts( array( 'instance' => $atts['instance'], // Ensure variables 'before_widget' => '', 'after_widget' => '', 'before_title' => '', 'after_title' => '', 'cache_id' => '', 'flush_cache' => isset( $_GET['delete-trans'] ), // Check for cache-buster ), isset( $atts['args'] ) ? (array) $atts['args'] : array(), self::$shortcode ); $instance = shortcode_atts( self::$defaults, ! empty( $atts['instance'] ) ? (array) $atts['instance'] : array(), self::$shortcode ); /* - If cache_id is not passed, we're not using the widget (but the shortcode), - so generate a hash cache id from the shortcode arguments */ if ( empty( $atts['cache_id'] ) ) { $atts['cache_id'] = md5( serialize( $atts ) ); } // Get from cache unless being requested not to $widget = ! $atts['flush_cache'] ? wp_cache_get( $atts['cache_id'], 'widget' ) : ''; // If $widget is empty, rebuild our cache if ( empty( $widget ) ) { $widget = ''; // Before widget hook $widget .= $atts['before_widget']; //$widget .= '
'; // Title $widget .= ( $instance['title'] ) ? $atts['before_title'] . esc_html( $instance['title'] ) . $atts['after_title'] : ''; $taxonomies = $atts['instance'][self::META_TAXONOMIES]; $widget .= '
'; foreach ($taxonomies as $key => $tax) { $widget .='
'.custom_dropdown(array("width"=>"100%","tax_slug"=>$tax, "show_option_all"=>get_taxonomy($tax)->labels->name)).'
'; } $widget .=''; $widget .='
'; //$widget .= wpautop( wp_kses_post( $instance['desc'] ) ); //$widget .= '
'; // After widget hook $widget .= $atts['after_widget']; wp_cache_set( $atts['cache_id'], $widget, 'widget', WEEK_IN_SECONDS ); } return $widget; } /** - Update form values as they are saved. * - @param array $new_instance New settings for this instance as input by the user. - @param array $old_instance Old settings for this instance. - @return array Settings to save or bool false to cancel saving. _/ public function update( $new_instance, $old_instance ) { $this->flush_widget_cache(); $sanitized = $this->cmb2( true )->get_sanitized_values( $new_instance ); return $sanitized; } /_* - Back-end widget form with defaults. * - @param array $instance Current settings. _/ public function form( $instance ) { // If there are no settings, set up defaults $this->_instance = wp_parse_args( (array) $instance, self::$defaults ); $cmb2 = $this->cmb2(); $cmb2->object_id( $this->option_name ); \CMB2_hookup::enqueue_cmb_css(); \CMB2_hookup::enqueue_cmb_js(); $cmb2->show_form(); ?> <style> .cmb-th + .cmb-td{ float:none !important; } </style> $this->option_name .'_box', // Option name is taken from the WP_Widget class. 'hookup' => false, 'show_on' => array( 'key' => 'options-page', // Tells CMB2 to handle this as an option 'value' => array( $this->option_name ) ), ), $this->option_name ); foreach ( $this->cmb2_fields as $field ) { if ( ! $saving ) { $field['id'] = $this->get_field_name( $field['id'] ); } $field['default_cb'] = array( $this, 'default_cb' ); $cmb2->add_field( $field ); } return $cmb2; } /_* - Sets the field default, or the field value. * - @param array $field_args CMB2 field args array - @param CMB2_Field $field CMB2 Field object. * - @return mixed Field value. */ public function default_cb( $field_args, $field ) { return isset( $this->_instance[ $field->args( 'id_key' ) ] ) ? $this->_instance[ $field->args( 'id_key' ) ] : null; } } 2016-10-28 15:01 GMT-02:00 Justin Sternberg notifications@github.com: > can you show me your code? > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub > https://github.com//pull/52#issuecomment-256973570, > or mute the thread > https://github.com/notifications/unsubscribe-auth/AH0FfKeTObnvvNq8lZBQdWTZ-Qk6JHRrks5q4ipdgaJpZM4KjfEH > .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants