How to change array properties within a class from member function - php

In the code below, I'm trying to alter the $widget_ops "classname" array, based on the value of the $line_above variable in the "widget" function. I'm not sure how to address the array to change the value.
Is this possible?
class my_widget extends WP_Widget {
function my_widget() {
$widget_ops = array('classname' => 'test', 'description' => __( "test") );
$control_ops = array('width' => 450, 'height' => 100, 'id_base' => 'my_widget' );
$this->WP_Widget('my_widget', __('Test: test'), $widget_ops, $control_ops);
}
function widget( $args, $instance ) {
extract($args);
$title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance );
$text = apply_filters( 'widget_text', $instance['text'], $instance );
$line_above = isset( $instance['line_above'] ) ? $instance['line_above'] : false;
if($line_above){
//TRYING TO ALTER THE CLASS HERE BASED ON VARIABLE
$widget_ops['classname'] .= " border-top";
}
}
}

You could make it global:
class my_widget extends WP_Widget {
private $widget_ops;
function my_widget() {
$this->widget_ops = array('classname' => 'test', 'description' => __( "test") );
$control_ops = array('width' => 450, 'height' => 100, 'id_base' => 'my_widget' );
$this->WP_Widget('my_widget', __('Test: test'), $this->widget_ops, $control_ops);
}
function widget( $args, $instance ) {
extract($args);
$title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance );
$text = apply_filters( 'widget_text', $instance['text'], $instance );
$line_above = isset( $instance['line_above'] ) ? $instance['line_above'] : false;
if($line_above){
//TRYING TO ALTER THE CLASS HERE BASED ON VARIABLE
$this->widget_ops['classname'] .= " border-top";
}
}
}

Related

List of checkboxes not saving in WordPress widget

I try to get the names of the pages in a loop.
In the widget, the page titles are displayed correctly, but the values are not saved.
How I can save the checked inputs?
Below Full Widget Code:
class Foo_Widget extends WP_Widget {
/** Register widget with WordPress.*/
function __construct() {
parent::__construct(
'foo_widget', // Base ID
esc_html__( 'Widget Title', 'text_domain' ), // Name
array( 'description' => esc_html__( 'A Foo Widget', 'text_domain' ), ) // Args
);
}
/*** Front-end display of widget.*/
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
echo esc_html__( 'Hello, World!', 'text_domain' );
echo $args['after_widget'];
}
/*** Back-end widget form.*/
public function form( $instance ) {
$mypages = get_pages( array(
'sort_column' => 'post_date',
'sort_order' => 'desc'
) );
foreach( $mypages as $page ) {
$title = $page->post_title;
$slug = $page->post_name;
?>
<p>
<label>
<input type='checkbox' name='the_pages' id='<?php echo $slug ?>' value='<?php echo esc_attr( $title ); ?>'><?php echo esc_attr( $title ); ?>
</label>
</p>
<? } ?>
<?php }
/*** Sanitize widget form values as they are saved.*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = $new_instance[ 'title' ];
return $instance;
}
}
// register Foo_Widget widget
function register_foo_widget() {
register_widget( 'Foo_Widget' );
}
add_action( 'widgets_init', 'register_foo_widget' );
?>
How should update() function looks like ?

Force a shipping method when a valid coupon is applied in Woocommerce

Using Woocommerce, I want to force a shipping method when a certain valid coupon is applied at the cart/checkout phase.
Below my code. It extends WC_Shipping_Method, gets the whole woocommerce coupon list, let admin to select one or more, save them and then, when a costumer applies one of those chosen coupons checking out, forces to use only one this shipping method.
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
/* Custom LocalPickup Class */
class WC_Shipping_LocalPickUp_On_Coupon extends WC_Shipping_Method {
public $requires = '';
public function __construct( $instance_id = 0 ) {
$this->id = 'localpickup_on_coupon';
$this->instance_id = absint( $instance_id );
$this->method_title = __( 'Pickup with Coupon', 'Valeo' );
$this->method_description = __( 'Pickup with Coupon', 'Valeo' );
$this->supports = array(
'shipping-zones',
'instance-settings',
'instance-settings-modal',
);
/*Getting Coupon List */
$args = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'asc',
'post_type' => 'shop_coupon',
'post_status' => 'publish',
);
$coupons = get_posts( $args );
$this->list = [];
foreach($coupons as $item){
$this->list[$item->post_name] = $item->post_title;
}
$this->init();
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
public function init() {
$this->init_form_fields();
$this->init_settings();
$this->title = $this->get_option( 'title' );
$this->requires = $this->get_option( 'requires' );
}
public function init_form_fields() {
$this->instance_form_fields = array(
'title' => array(
'title' => __( 'Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => $this->method_title,
'desc_tip' => true,
),
'requires' => array(
'title' => __( 'Il ritiro in sede richiede...', 'Valeo' ),
'type' => 'multiselect',
'class' => 'valeo_multiselect',
'default' => '',
'options' => $this->list
),
);
}
public function is_available( $package ) {
$has_coupon = false;
$coupons = WC()->cart->get_coupons();
if(!empty($coupons)) {
$couponKey = array_keys($coupons)[0];
if ( in_array( $couponKey, $this->requires ) ) {
if ( $coupons ) {
foreach ( $coupons as $code => $coupon ) {
if ( $coupon->is_valid() ) {
$has_coupon = true;
break;
}
}
}
}
}
return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $has_coupon, $package, $this );
}
public function calculate_shipping( $package = array() ) {
$rate = array(
'id' => $this->get_rate_id(),
'label' => $this->title,
'cost' => 0,
'taxes' => 0,
'package' => $package,
);
$this->add_rate($rate);
do_action( 'woocommerce_' . $this->id . '_shipping_add_rate', $this, $rate );
}
}
add_filter( 'woocommerce_shipping_methods', 'register_devso_method' );
function register_devso_method( $methods ) {
$methods[ 'localpickup_on_coupon' ] = 'WC_Shipping_LocalPickUp_On_Coupon';
return $methods;
}
}
Here the filter that checks if customer has inserted the chosen coupon/s and (in a "bad" way) removes all woocommerce package rates but the one I declared in the class.
function filter_woocommerce_package_rates( $array ) {
$check = FALSE;
foreach($array as $index => $value){
if(strpos($index, 'localpickup_on_coupon') !== false) { $check = TRUE; }
}
if($check){
foreach($array as $index => $value){
if(!(strpos($index, 'localpickup_on_coupon') !== false)) { unset($array[$index]); }
}
}
return $array;
};
// add the filter
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 1 );
Is there a better way to do this?

Wordpress create widget area in a widget

I am trying to insert a custom widget area in another widget, but don't know where to start. Do I need to register new widget areas in widget class constructor with unique id's ?
What I am trying to achieve:
Insert a section widget which contains a widget area.
Insert custom widgets into that section with the widget area.
<?php
function dov_load_widget_default( ) {
register_widget( 'section' );
}
add_action( 'widgets_init', 'dov_load_widget_default' );
class section extends WP_Widget {
function __construct( ) {
parent::__construct(
'section',
__( 'Portfolio sections', 'dov_portfolio' ),
array( 'description' => __( 'Default section for widgets', 'dov_portfolio' ) )
);
}
// Creating widget front-end
public function widget( $args, $instance ) {
}
// Widget Backend
public function form( $instance ) {
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array( );
return $instance;
}
}
?>
Don't know if it's the right way but it seems to work.
function dov_load_sidebars( ) {
register_sidebar( array(
'id' => 'portfolio_sections',
'name' => __( 'Portfolio sections', 'dov_portfolio' )
));
$sections = wp_get_sidebars_widgets( );
if( isset( $sections[ 'portfolio_sections' ] ) ) :
$sections = $sections[ 'portfolio_sections' ];
foreach( $sections as $section ) :
register_sidebar( array(
'id' => $section,
'name' => __( 'Portfolio section', 'dov_portfolio' ) . ': ' . $section
));
endforeach;
endif;
}
add_action( 'widgets_init', 'dov_load_sidebars' );
EDIT: After inserting the section widget the widget areas don't refresh. How can I refresh them or should I add them using js ?

Extend Child Class of one plugin into another plugin

I am using [Plugin: Visualizer: Charts and Graphs]
In this plugin there is class
class Visualizer_Module_Frontend extends Visualizer_Module {
const NAME = __CLASS__;
private $_charts = array();
public function __construct( Visualizer_Plugin $plugin ) {
parent::__construct( $plugin );
$this->_addAction( 'wp_enqueue_scripts', 'enqueueScripts' );
$this->_addShortcode( 'visualizer', 'renderChart' );
// add do_shortocde hook for widget_text filter
if ( !has_filter( 'widget_text', 'do_shortcode' ) ) {
add_filter( 'widget_text', 'do_shortcode' );
}
// add do_shortcode hook for term_description filter
if ( !has_filter( 'term_description', 'do_shortcode' ) ) {
add_filter( 'term_description', 'do_shortcode' );
}
}
public function enqueueScripts() {
wp_register_script( 'visualizer-google-jsapi', '//www.google.com/jsapi', array(), null, true );
wp_register_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( 'visualizer-google-jsapi', 'jquery' ), Visualizer_Plugin::VERSION, true );
}
public function renderChart( $atts ) {
$atts = shortcode_atts( array(
'id' => false, // chart id
'class' => false, // chart class
'series' => false, // series filter hook
'data' => false, // data filter hook
'settings' => false, // data filter hook
), $atts );
// if empty id or chart does not exists, then return empty string
if ( !$atts['id'] || !( $chart = get_post( $atts['id'] ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
return '';
}
$id = 'visualizer-' . $atts['id'];
$class = apply_filters( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS, $atts['class'], $atts['id'] );
$class = !empty( $class ) ? ' class="' . $class . '"' : '';
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
// faetch and update settings
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
if ( empty( $settings['height'] ) ) {
$settings['height'] = '400';
}
// handle series filter hooks
$series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
if ( !empty( $atts['series'] ) ) {
$series = apply_filters( $atts['series'], $series, $chart->ID, $type );
}
// handle settings filter hooks
$settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type );
if ( !empty( $atts['settings'] ) ) {
$settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type );
}
// handle data filter hooks
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type );
if ( !empty( $atts['data'] ) ) {
$data = apply_filters( $atts['data'], $data, $chart->ID, $type );
}
// add chart to the array
$this->_charts[$id] = array(
'type' => $type,
'series' => $series,
'settings' => $settings,
'data' => $data,
);
// enqueue visualizer render and update render localizations
wp_enqueue_script( 'visualizer-render' );
wp_localize_script( 'visualizer-render', 'visualizer', array( 'charts' => $this->_charts ) );
return '<div id="' . $id . '"' . $class . '></div>';
} }
I want to extent this class so that i can override the function renderChart().
But I want to do this with another custom plugin.

wordpress woocommerce interfax sends fax with a missing field

I am not a php programmer, and it takes me forever to figure out why. So hopefully SO could help me out. Basically, I installed a woocommerce plugin called interfax-woocommerce, which is used to send order completion email to fax number. On the documentation, it says the plugin uses order completion template to send the fax. It does include everything on the order email template except leaving the order note field out. I believe it must have something to do with the plugin. maybe it passes the order array without including that particular order note field? I can not figure out why and how to fix it. Below is the source code of interfax plugin
if ( ! defined( 'ABSPATH' ) ) exit;
class WooCommerce_InterFax_Integration {
private $dir;
private $file;
private $assets_dir;
private $assets_url;
private $settings;
private $interfax_class;
private $username;
private $password;
private $store_fax_number;
private $template_html;
private $heading;
private $send_store_fax;
private $order;
private $fax_number;
public function __construct( $file ) {
$this->dir = dirname( $file );
$this->file = $file;
$this->assets_dir = trailingslashit( $this->dir ) . 'assets';
$this->assets_url = esc_url( trailingslashit( plugins_url( '/assets/', $file ) ) );
// Add settings link to plugin page
add_filter( 'plugin_action_links_' . plugin_basename( $this->file ), array( $this, 'add_settings_link' ) );
// Get integration settings
$this->settings = get_option( 'woocommerce_interfax_settings' );
// Only handle actions if integration is enabled
if( $this->settings['wcif_enable'] == 'yes' ) {
$this->interfax_class = 'http://ws.interfax.net/dfs.asmx?wsdl';
$this->username = $this->settings['wcif_username'];
$this->password = $this->settings['wcif_password'];
$this->store_fax_number = $this->settings['wcif_fax_number'];
$this->template_html = 'emails/customer-completed-order.php';
$this->heading = __( 'Your order is complete', 'woocommerce' );
$this->send_store_fax = true;
// Add fax number field to checkout
add_filter( 'woocommerce_checkout_fields', array( $this, 'add_checkout_field' ) );
// Send fax on selected order status (deault to 'completed' if no status is selected)
if( $this->settings['wcif_fax_status'] ) {
$fax_status = $this->settings['wcif_fax_status'];
} else {
$fax_status = 'completed';
}
add_action( 'woocommerce_order_status_' . $fax_status, array( $this, 'trigger' ) );
// Add order action to dashboard
add_filter( 'woocommerce_order_actions', array( $this, 'add_order_action' ) );
// Handle order actions
add_action( 'woocommerce_order_action_send_customer_fax', array( $this, 'process_order_action_customer' ) );
add_action( 'woocommerce_order_action_send_store_fax', array( $this, 'process_order_action_store' ) );
// Add fax number field to user profile page in WP dashboard
add_filter( 'woocommerce_customer_meta_fields', array( $this, 'add_user_meta_field' ) );
// Add fax number field to edit billing address page
add_filter( 'woocommerce_billing_fields', array( $this, 'add_address_field' ), 10, 2 );
}
// Handle localisation
$this->load_plugin_textdomain();
add_action( 'init', array( $this, 'load_localisation' ), 0 );
}
public function trigger( $order_id ) {
if ( $order_id ) {
$this->order = new WC_Order( $order_id );
// Send fax to store owner
if( $this->store_fax_number && strlen( $this->store_fax_number ) > 0 ) {
$this->send_store_fax();
}
// Send fax to customer
$this->fax_number = get_post_meta( $order_id, '_billing_fax', true );
if( $this->fax_number && strlen( $this->fax_number ) > 0 ) {
$this->send_customer_fax();
}
}
}
private function send_customer_fax() {
$interfax_client = new SoapClient( $this->interfax_class );
$params->Username = $this->username;
$params->Password = $this->password;
$params->FaxNumber = $this->fax_number;
$params->Data = $this->get_fax_content();
$params->FileType = 'HTML';
$result = $interfax_client->SendCharFax( $params );
if( isset( $result->SendCharFaxResult ) && $result->SendCharFaxResult > 0 ) {
return true;
}
return false;
}
private function send_store_fax() {
$interfax_client = new SoapClient( $this->interfax_class );
$params->Username = $this->username;
$params->Password = $this->password;
$params->FaxNumber = $this->store_fax_number;
$params->Data = $this->get_fax_content();
$params->FileType = 'HTML';
$result = $interfax_client->SendCharFax( $params );
if( isset( $result->SendCharFaxResult ) && $result->SendCharFaxResult > 0 ) {
return true;
}
return false;
}
public function get_fax_content() {
global $woocommerce;
ob_start();
$template_data = array(
'order' => $this->order,
'email_heading' => $this->heading
);
if( version_compare( $woocommerce->version, '2.1-beta-1', ">=" ) ) {
wc_get_template( $this->template_html, $template_data );
} else {
woocommerce_get_template( $this->template_html, $template_data );
}
return ob_get_clean();
}
public function add_checkout_field( $fields ) {
$fields['billing']['billing_fax'] = array(
'label' => __( 'Fax Number (including country code)', 'wc_interfax' ),
'placeholder' => _x( 'e.g. +27861234567', 'placeholder', 'wc_interfax' ),
'required' => false,
'class' => array( 'form-row' ),
'clear' => false
);
return $fields;
}
public function add_order_action( $actions ) {
$actions['send_customer_fax'] = 'Send customer fax';
$actions['send_store_fax'] = 'Send store fax';
return $actions;
}
public function process_order_action_customer( $order ) {
$this->fax_number = get_post_meta( $order->id, '_billing_fax', true );
if( $this->fax_number && strlen( $this->fax_number ) > 0 ) {
$this->order = $order;
$this->send_customer_fax();
}
}
public function process_order_action_store( $order ) {
if( $this->store_fax_number && strlen( $this->store_fax_number ) > 0 ) {
$this->order = $order;
$this->send_store_fax();
}
}
public function add_user_meta_field( $fields ) {
$fields['billing']['fields']['billing_fax'] = array(
'label' => __( 'Fax number', 'wc_interfax' ),
'description' => ''
);
return $fields;
}
public function add_address_field( $fields, $country ) {
$fields['billing_fax'] = array(
'label' => __( 'Fax Number (including country code)', 'wc_interfax' ),
'placeholder' => _x( 'e.g. +27861234567', 'placeholder', 'wc_interfax' ),
'required' => false,
'class' => array( 'form-row' ),
'clear' => true
);
return $fields;
}
public function add_settings_link( $links ) {
$settings_link = '' . __( 'Configure', 'wc_interfax' ) . '';
array_unshift( $links, $settings_link );
return $links;
}
public function load_localisation () {
load_plugin_textdomain( 'wc_interfax' , false , dirname( plugin_basename( $this->file ) ) . '/lang/' );
}
public function load_plugin_textdomain () {
$domain = 'wc_interfax';
$locale = apply_filters( 'plugin_locale' , get_locale() , $domain );
load_textdomain( $domain , WP_LANG_DIR . '/' . $domain . '/' . $domain . '-' . $locale . '.mo' );
load_plugin_textdomain( $domain , FALSE , dirname( plugin_basename( $this->file ) ) . '/lang/' );
}
Here is an example of adding the customer's note (which is the order post's "excerpt") to the bottom of all emails:
add_action( 'woocommerce_email_after_order_table' , 'so_28333042_add_customer_note' );
function so_28333042_add_customer_note( $order ){
$post = get_post( $order->id );
if( $post->post_excerpt != '' ){
echo "<strong>Customer Note:</strong><br/>" . wp_kses_post( $post->post_excerpt );
}
}
The woocommerce_email_after_order_table hook is definitely available in WooCommerce 2.3, but I think it is available in 2.2+ as well.
Alternatively, you could copy the customer-completed-order.php template into your theme and work directly on it.

Categories