instantiate a class from plugin in WordPress - wordpress

I have a question about class written in the plugin. Let's say I have this class.
class Woo {
public static init(){
register_activation_hook( __FILE__, array( __CLASS__, 'woo_activate' ));
add_action( 'admin_menu', array(__CLASS__, 'woo_register'));
}
Woo::init();
add_action( 'plugins_loaded', array( 'Woo', 'init' ) );
My question is why put Woo::init(); there where we already have the add_action( 'plugins_loaded', array( 'Woo', 'init' ) );
Are both executed? can woo::init() be deleted and it will work fine? and vice versa?

Related

Wordpress remove_action within class

I am trying to remove an action added by the Woocommerce Memberships plugin. Tracing back the actions contains functions it is initially added to the hook under
class WC_Memberships_Frontend {
public function __construct() {
add_action( 'woocommerce_thankyou', array( $this, 'maybe_render_thank_you_content' ), 9 );
But this class is called by a private function and is a protected by the variable that its parent class declaration (as far as I can tell)
class WC_Memberships extends Framework\SV_WC_Plugin {
protected $frontend;
private function frontend_includes() {
// init shortcodes
require_once( $this->get_plugin_path() . '/includes/class-wc-memberships-shortcodes.php' );
\WC_Memberships_Shortcodes::initialize();
// load front end
$this->frontend = $this->load_class( '/includes/frontend/class-wc-memberships-frontend.php', 'WC_Memberships_Frontend' );
}
I have search and tried a number of ways to remove this
remove_action( 'woocommerce_thankyou', array( 'WC_Memberships', 'maybe_render_thank_you_content' ), 9 );
remove_action( 'woocommerce_thankyou', array( 'WC_Memberships_Frontend', 'maybe_render_thank_you_content' ), 9 );
global $WC_Memberships;
remove_action( 'woocommerce_thankyou', array( $WC_Memberships, 'maybe_render_thank_you_content' ), 9 );
global $WC_Memberships_Frontend;
remove_action( 'woocommerce_thankyou', array( $WC_Memberships_Frontend, 'maybe_render_thank_you_content' ), 9 );
None of the above work, and other ways of trying to call WC_Memberships()->frontend; throw errors like 'Cannot access protected property WC_Memberships::$frontend'
Im not sure if the private function or protected variable are getting in the way, or if Im just not understanding something about removing an action within a class or nested classes but help would be greatly appreciated.
Edit:
Based on the code found here Ive tried
remove_action( 'woocommerce_thankyou', array( wc_memberships()->get_frontend_instance(), 'maybe_render_thank_you_content', 9 ) );
but still no success.
My issue was trying to figure out how to match the $this in the original add_action add_action( 'woocommerce_thankyou', array( $this, 'maybe_render_thank_you_content' ), 9 ); to remove it.
I finally stumbled on this chunk of code which showed me how to reference the proper instance of the class.
Final working code:
remove_action( 'woocommerce_thankyou', array( wc_memberships()->get_frontend_instance(), 'maybe_render_thank_you_content', 9 ) );
As you probably know, it's usually not good practice to modify core or plugin files. Using a modification to the answer here , you could try adding to your themes functions.php file
function remove_aggravating_wcactions()
{
remove_action( 'woocommerce_thankyou', 'maybe_render_thank_you_content', 19 );
remove_action( 'woocommerce_thankyou', 'maybe_some_other_content', 20);
...
}
add_action('template_redirect','remove_aggravating_wcactions');

Overwriting a filter in plugin using WordPress

I have the following filter in a WordPress plugin named 'JobBoard Package'
function apply_job_validate($validate){
$package = jb_package_get_current_package();
if(!$package){
jb_notice_add(esc_html__('You need to purchase a package before applying job.', 'jobboard-package'), 'error');
return true;
}
$applied = JB()->candidate->count_applied_all();
$limit = get_post_meta($package->ID, '_apply', true);
if($applied >= $limit){
jb_notice_add(esc_html__('Apply job limited, you can update your package.', 'jobboard-package'), 'error');
return true;
}
return $validate;
}
Being called at the top of the plugin as follows:
private function actions(){
add_action( 'wp_enqueue_scripts', array($this, 'add_scripts'));
add_action( 'admin_menu', array ($this, 'get_menu_notice') , 100);
add_action( 'admin_enqueue_scripts', array($this, 'add_admin_scripts'));
add_action( 'save_post_jobboard-post-jobs', array($this, 'save_post'));
add_filter( 'jobboard_query_endpoint_args', array($this, 'add_endpoint'));
add_filter( 'jobboard_query_endpoint_package_title', array($this, 'add_endpoint_package_title'));
add_filter( 'jobboard_query_endpoint_transactions_title', array($this, 'add_endpoint_transactions_title'));
add_filter( 'jobboard_employer_navigation_args', array($this, 'add_endpoint_menu'));
add_filter( 'jobboard_candidate_navigation_args', array($this, 'add_endpoint_menu'));
add_action( 'jobboard_endpoint_employer_new', array($this, 'get_template_add_new'), 0);
add_action( 'jobboard_endpoint_employer_package', array($this, 'get_template_package') );
add_action( 'jobboard_endpoint_candidate_package', array($this, 'get_template_package') );
add_filter( 'jobboard_form_handler_validate_add_job', array($this, 'add_new_job_validate'));
add_filter( 'jobboard_form_handler_validate_apply_job', array($this, 'apply_job_validate'));
}
I have tried disabling apply_job_validate via the functions.php and as a plugin using the following code and its variations with no luck and would really appreciate some help.
if( class_exists('JB_Package' ) ){
//This should work in whatever case
remove_filter('jobboard_form_handler_validate_apply_job', array( 'JB_Package', 'apply_job_validate'));
//or Instantiating a new instance
//remove_filter('jobboard_form_handler_validate_apply_job', array( new JB_Package(), 'apply_job_validate'));
//or Targeting the specific instance, not tested
//remove_filter('jobboard_form_handler_validate_apply_job', array( JB_Package::get_instance(), 'apply_job_validate'));
}
I have also tried it as follows:
function remove_package() {
if (class_exists('JB_Package')) {
remove_filter('jobboard_form_handler_validate_apply_job', array( 'JB_Package', 'apply_job_validate'));
}
}
add_action('plugins_loaded','remove_package');
Any help would be appreciated. Many thanks!
You should use a later action hook, like after_setup_theme. You're hooking into plugins_loaded, which is fired before your functions.php file is parsed. Try:
function remove_package() {
if (class_exists('JB_Package')) {
remove_filter('jobboard_form_handler_validate_apply_job', array( 'JB_Package', 'apply_job_validate'));
}
}
add_action('after_setup_theme','remove_package');
However, you should check to see when the 'JB_Package' class is hooked (if it's hooked at all). If it's hooked at init, then you need to hook into something later like wp_loaded.
//if class 'JB_Package' is hooked at 'init'
add_action('wp_loaded','remove_package');

remove_action from a plugin class on a different namespace

Good evening to all, i've a little problem removing an action from wp_head that was added by Wordpress Download Manager. That's the code from the plugin:
class WordPressDownloadManager{
function __construct(){
register_activation_hook(__FILE__, array($this, 'Install'));
add_action( 'init', array($this, 'registerPostTypeTaxonomy'), 1 );
add_action( 'plugins_loaded', array($this, 'loadTextdomain') );
add_action( 'wp_enqueue_scripts', array($this, 'EnqueueScripts') );
add_action( 'wp_head', array($this, 'wpHead') );
add_action( 'wp_footer', array($this, 'wpFooter') );
spl_autoload_register( array( $this, 'AutoLoad' ) );
new \WPDM\libs\UserDashboard();
new \WPDM\libs\Apply();
new \WPDM\admin\WordPressDownloadManagerAdmin();
new \WPDM\ShortCodes();
}
and this is the code i'm using to remove it:
function remove_wpdm() {
remove_action('wp_head', array('WordPressDownloadManager', 'wpHead'));
}
add_action('wp_head', 'remove_wpdm');
without effects.. How can i solve this? this class is in a different namespace called WPDM. Thank you in advance for any help.
Best regards,
Domenico
Try below code
function remove_wpdm() {
remove_action('wp_head', array('WordPressDownloadManager', 'wpHead'));
}
add_action('init', 'remove_wpdm');

Adding a receipt page on woocommerce

I am developing a payment gateway for woocommerce.
I read many tutorial of how I can manage to do it but I can't figure out what this line of code mean:
add_action( 'woocommerce_receipt_paypal', array( $this, 'receipt_page' ) );
Especially, there is no function called woocommerce_receipt_paypal and how can I reproduce it in my plugin.
You have to replace
add_action( 'woocommerce_receipt_paypal', array( $this, 'receipt_page' ) );
with
add_action( 'woocommerce_receipt_' . $this->id, array( $this, 'receipt_page' ) );
where id is the one you set in __construct() function.

register_activation_hook in construct doesn't work

I have an question about register_activation_hook in construct.
I have read, that this should be possible, but I don't know, currently nothing will be written to the error_log (for debugging).
Look here, the author say it should work http://www.plulz.com/how-to-create-a-plugin-for-wordpress
Here's my code
<?php
abstract class LW_Capability{
const NAME = 'Capability';
public function __construct(){
register_activation_hook(
__FILE__,
array(
$this,
'activate'
)
);
register_deactivation_hook(
__FILE__,
array(
$this,
'deactivate'
)
);
}
public function activate(){
error_log('LW_Capability->activate');
}
public function deactivate(){
error_log('LW_Capability->deactivate');
}
}
class CapabilityEditRessource extends LW_Capability{
const NAME = 'EditRessource';
}
?>
What do I do wrong? If I add an "die('parent')" to the LW_Capability, it will die. The Plugin can't be activated then (WP Blocks because of output).
Anyone here who does somthing similar?
Would be happy to hear from you.
Regards,
Oli
register_activation_hook(
__FILE__,
array(
$this,
'activate'
)
);
register_deactivation_hook(
__FILE__,
array(
$this,
'deactivate'
)
);
could just be used in the main-file of the plugin ^^
I modifyed it to
register_activation_hook(
__FILE__,
array(
$MyPluginInstance,
'activate'
)
);
register_deactivation_hook(
__FILE__,
array(
$MyPluginInstance,
'deactivate'
)
);
And this functions calls manually the register / unregister-functions of the capability-class

Resources