Can't use self in static method in php - php

I can't use self in a static method , it gives me this error message :
Fatal error: Using $this when not in object context in C:\xampp\htdocs\wordpress\wp-content\plugins\dw-usercp\usercp.php on line 136
here is the source code :
class dw_usercp
{
public static function plugin_activated() {
self::create_plugin_pages();
}
public function create_plugin_pages() {
$pages = array(
'signin' => array(
'title' => __( 'Sign In', 'dw-usercp' ),
'content' => '[dwusercp-sigin-form]',
'option_id' => 'login_page'
),
'user-account' => array(
'title' => __( 'Your Account', 'dw-usercp' ),
'content' => '[dwusercp-info]',
'option_id' => 'user_account_page'
),
'edit-user-info' => array(
'title' => __( 'Edit User Info', 'dw-usercp' ),
'content' => '[dwusercp-edit-info]',
'option_id' => 'user_editinfo_page'
),
'profile' => array(
'title' => __( 'User profile', 'dw-usercp' ),
'content' => '[dwusercp-profile]',
'option_id' => 'profile_page'
),
'signup' => array(
'title' => __( 'Sign Up', 'dw-usercp' ),
'content' => '[dwusercp-signup-form]',
'option_id' => 'register_page'
),
'user-lost-password' => array(
'title' => __( 'Forgot Your Password?', 'dw-usercp' ),
'content' => '[dwusercp-password-lost-form]',
'option_id' => 'lost_password_page'
),
'user-password-reset' => array(
'title' => __( 'Pick a New Password', 'dw-usercp' ),
'content' => '[dwusercp-password-reset-form]',
'option_id' => 'password_reset_page'
)
);
foreach( $pages as $slug => $page ) {
$query = new WP_Query( 'pagename=' . $slug );
if ( ! $query->have_posts() ) {
// Add the page using the data from the array above
$post_id = wp_insert_post(
array(
'post_content' => $page['content'],
'post_name' => $slug,
'post_title' => $page['title'],
'post_status' => 'publish',
'post_type' => 'page',
'ping_status' => 'closed',
'comment_status' => 'closed',
)
);
$this->update_plugin_option( $page['option_id'], $post_id ); // this is the line 136 that the error message says
}
}
}
/**
* Update plugin option
*
* #param string $field option id
* #param mixed $value option new value
* #return bool
*/
public function update_plugin_option( $field, $value ) {
$options = get_option("dw_usercp_options");
$options[$field] = $value;
update_option( "dw_usercp_options", $options );
}
}
$dw_usercp = new dw_usercp();
register_activation_hook( __FILE__, array( 'dw_usercp', 'plugin_activated' ) );
how do i call the create_plugin_pages() correctly then?
the plugin_activated() has to be static as Wordpress says

Inside a static function, you're not in an instance of the class. You could:
instantiate an instance of the class and call the function
Pass an instatiated object into the static funtion
Make the create_plugin_pagesfunction static and call it with static.
Convert plugin_activated to not be static (MY VOTE)
The static option won't work though since you call $this inside create_plugin_pages. So you'll need to go the instatiation route.
Non-static
public function plugin_activated() {
$this->create_plugin_pages();
}
Here's the passing in an object version
public static function plugin_activated(dw_usercp $a_dw_usercp) {
$a_dw_usercp->create_plugin_pages();
}

Its because you are using "$this" variable when you're in static context (did you read the error message?)
When in static context, use for methods:
self::method(args);
or
self::$attr for variables (attributes)

$this->update_plugin_option( $page['option_id'], $post_id );
is not self, as referenced in your title. You should be using:
self::update_plugin_option( $page['option_id'], $post_id );

Related

Problem with foreach array on select option on woocmmerce function class

I create a function to add different shiping prices foreach user, all works fine for only one user (the first on the list), but i can't get to make the select options foreach user, it only shows 1 option with all the user in it.
the problem is integrate the foreach in here: 'test' => array. It creates the html elemnt but only 1 and not 1 foreach user
my code:
class WC_Shipping_click extends WC_Shipping_Method {
public function __construct( $instance_id = 0 ) {
$args = array(
'role' => '',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users( $args );
foreach ( $users as $user ){
$userinfo .= '['.$user->precio.','.$user->first_name.'] ';
}
$this->id = 'click_method';
$this->instance_id = absint( $instance_id );
$this->method_title = __( 'Click Shipping Method' );
$this->method_description = __( 'Click Shipping' );
$this->supports = array(
'shipping-zones',
'instance-settings',
'instance-settings-modal',
);
$this->instance_form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable' ),
'type' => 'checkbox',
'label' => __( 'Enable this shipping method' ),
'default' => 'yes',
),
'test' => array(
'title' => __( 'Prueba de array' ),
'description' => $user->first_name,
'type' => 'select',
'options' => array( ''.$userinfo.'' => ''.$userinfo'',),
'label' => __( 'Some field' ),
'required' => true,
)
)
);
$this->enabled = $this->get_option( 'enabled' );
$this->prueba = $this->get_option( 'prueba' );
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* #param array $package (default: array())
*/
public function calculate_shipping( $package = array() ) {
$this->add_rate( array(
'id' => $this->id . $this->instance_id,
'label' => $this->title,
'cost' => $this->test,
) );
}
}
This is the scope of your foreach()
foreach ( $users as $user ){
$userinfo .= '['.$user->precio.','.$user->first_name.'] ';
}
Nothing that is below will fall into this foreach() loop.

how to limit cmb2 to specific page templates

I want cmb2 to be displayed in posts that have a movie template
I use this code :
$cmb_review = new_cmb2_box( array(
'id' => $prefix . 'metabox',
'title' => __( "My title", 'cmb2' ),
'object_types' => array( 'post'),
'show_on' => array( 'key' => 'page-template', 'value' => 'movie.php' )
));
but that doesn't work
I also tried this code but it didn't work :
function maybe_show($cmb_review ) {
$template = get_post_meta( $cmb_review ->object_id(), 'page-template', true );
if ( 'movie.php' === $template ) {
return true;
}
return false;
}
$cmb_review = new_cmb2_box( array(
'id' => $prefix . 'metabox',
'title' => __( "My title", 'cmb2' ),
'object_types' => array( 'post'),
'show_on_cb' => 'maybe_show'
));
Your meta box object type is post and is your template page for post too?
Make sure both match.

Else statement being executed in custom shortcode

I have the following shortcode mapped to display a dropdown field in WPBakery:
array(
'type' => 'dropdown',
'heading' => esc_html__( 'Type of container', 'js_composer' ),
'param_name' => 'container_type',
'value' => array(
'Container' => 'container',
'Container fluid (full width)' => 'container-fluid',
),
'std' => 'container',
'description' => esc_html__( 'Select whether you want the content to be contained or full width on the screen.', 'js_composer' ),
)
It is being initialised in another file where its attributes and markup is defined:
<?php
/**
* Shortcode attributes
* #var $container_type
*/
class vcRow extends WPBakeryShortCode {
function __construct() {
add_action( 'init', array( $this, 'vc_row_mapping' ) );
add_shortcode( 'vc_row', array( $this, 'vc_row' ) );
}
public function vc_text_mapping() {
vc_map(
array(
'name' => __('row', 'text-domain'),
'base' => 'vc_row',
'description' => __('', 'text-domain'),
'params' => array(
array(
'type' => 'dropdown',
'heading' => esc_html__( 'Type of container', 'js_composer' ),
'param_name' => 'container_type',
'value' => array(
'Container' => 'container',
'Container fluid (full width)' => 'container-fluid',
),
'std' => 'container',
),
);
}
public function vc_row($atts, $content) {
extract(
shortcode_atts(
array(
'container_type' => '',
),
$atts
)
);
if ($container_type == "container-fluid"){
echo "container-fluid";
} else {
echo "container";
}
echo "<p>".$container_type."</p>";
}
}
new vcRow();?>
One of my fields is set to 'container-fluid, but anechoof$container_type` is showing "container" (meaning the else statement is being executed. Unsure why?

How to correctly override a class method in Wordpress?

How to correctly override a class method of this class preview_handler (), whether it is possible to make it in a child class?
class WP_some_class {
public function __construct() {
add_action( 'wp', array( $this, 'process' ) );
$this->steps = (array) apply_filters( 'submit_job_steps', array(
'submit' => array(
'name' => __( 'Submit Details', 'wp-job-manager' ),
'view' => array( $this, 'submit' ),
'handler' => array( $this, 'submit_handler' ),
'priority' => 10
),
'preview' => array(
'name' => __( 'Preview', 'wp-job-manager' ),
'view' => array( $this, 'preview' ),
'handler' => array( $this, 'preview_handler' ),
'priority' => 20
),
'done' => array(
'name' => __( 'Done', 'wp-job-manager' ),
'view' => array( $this, 'done' ),
'priority' => 30
)
) );
public function preview_handler() {
// .. some code
}
}
You can simply extend the class.
class Extending_class extends WP_some_class
{
public function preview_handler()
{
// Your code
}
}
Beware that you have to mantain the same method parameters (no one, in this case) as the original class.
From the PHP documentation:
[...] when you extend a class, the subclass inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality.

Function Not Recognized in Private PHP Array

I have the following code based on this great template for making custom WordPress widgets:
<?php class DRCC_Feat extends WP_Widget {
function dr_post_select_list() {
$drcc_posts_args = array(
'post_type' => array( 'post', 'page', 'tribe_events' ),
'numberposts' => -1,
'orderby' => 'title',
'order' => 'ASC'
);
$drcc_posts = get_posts( $drcc_posts_args );
$dr_posts_array = array();
foreach( $drcc_posts as $post ) {
$dr_posts_array[$post->ID] = $post->post_title;
}
return $dr_posts_array;
}
protected $widget = array(
'description' => 'Custom widget for my client.',
'do_wrapper' => true,
'view' => false,
'fields' => array(
array(
'name' => 'Post to Feature',
'desc' => 'Enter the IDs of any posts, pages, etc. If more than one, separate with commas.',
'id' => 'dr_feat_ids',
'type' => 'select',
'options' => dr_post_select_list(),
'std' => ''
)
)
);
// some more stuff here, but the error is above and the code works when I make 'options' somethings hard-coded.
} ?>
I am trying to call dr_post_select_list() in the protected $widget array to dynamically generate a list of posts, but I get the error Parse error: syntax error, unexpected '(', expecting ')' in reference to the line with the dr_post_select_list() function in it. It's as if it doesn't recognize that it's a function.
I've tried the function elsewhere and it works fine. I've tried making the array public and that doesn't change anything. I've tried saving the function output in an array and putting the variable in the array
I get the sense that I'm doing something fundamentally wrong.
tl;dr - method called in array in a class doesn't run (or seem to be recognized as a method).
You're missing a closing } for your function.
Replace your code with the following:
<?php
class DRCC_Feat extends WP_Widget {
protected $widget = array(
'description' => 'Custom widget for my client.',
'do_wrapper' => true,
'view' => false,
'fields' => array(
array(
'name' => 'Post to Feature',
'desc' => 'Enter the IDs of any posts, pages, etc. If more than one, separate with commas.',
'id' => 'dr_feat_ids',
'type' => 'select',
'options' => 'dr_post_select_list',
'std' => ''
)
)
);
function dr_post_select_list() {
$drcc_posts_args = array(
'post_type' => array( 'post', 'page', 'tribe_events' ),
'numberposts' => -1,
'orderby' => 'title',
'order' => 'ASC'
);
$drcc_posts = get_posts( $drcc_posts_args );
$dr_posts_array = array();
foreach( $drcc_posts as $post ) {
$dr_posts_array[$post->ID] = $post->post_title;
}
return $dr_posts_array;
}
}
Edited: Moved all code inside class, corrected error. Have a look at this post as well in regards to storing functions in an array. Slightly different than say JS.
Can you store a function in a PHP array?

Categories