Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+4 votes
1.2k views
in Plugins by
Found one issue where plugin layer has custom query for custom column and if column and  plugin doesn't exists definatly it will gives an error. But in some case it mess up the layout too.

So is there any way to check if plugin exists same like function_exists() ? so that code we can wrap around that function and if plugin doesn't exists than just ignore it.
Q2A version: plugin-api, check, conditional
by
Hi Jatin, could you find another solution? I am still wondering which approach is the best. The function_exists() approach below is not recommended as the function will only exist if the plugin has been called beforehand. Really need a plugin_exists() function!
by
Find my below answer.. may be help you

2 Answers

0 votes
by

you can use function_exists with one of the functions' names in the plugin.

For checking if the badges plugin is installed, for instance, I am using this in another plugin:

                // output of badges, needs badges plugin
                echo (function_exists('qa_badge_plugin_user_widget') ? '<br/>'.qa_badge_plugin_user_widget($handle) : '');

 

where qa_badge_plugin_user_widget' is the function of badges plugin.

by
I think this can be alternative but no exactly what I am looking.. What I want to specifically check either pluign exist or not. And that will apply to every single piece of code for my all files. Means instead of wrapping individual function to check I want one function same like function_exists where it can check plugin_exists(). This is not only to check for function. But if I have custom query or something more than juts function where function_exists() may not works.
+1 vote
by
edited by

Since Q2A Plugin is all about the class and any function out of the class, those too have to include within the class. What I believe that if we check if the plugin class exists that may be alternative of the plugin_exists() function.

But here is one more problem. Q2A plugin can contain multiple class for layer, module etc.. However layer will use qa_html_theme_base class and so it is not good idea to check for that class, since it is always exists. But the plugin class.

See below. I haven't practically tried this but logically it should work

<?php
//registering plugin stuffs
qa_register_plugin_layer('q2am-item-output.php', 'Q2AM Item Layer');
qa_register_plugin_module('page', 'q2am-item-page.php', 'q2am_item_page', 'Q2AM Item Page');
qa_register_plugin_module('module', 'q2am-item-admin.php', 'q2am_item_admin', 'Q2AM Item Settings');
qa_register_plugin_phrases('q2am-Item-lang.php', 'q2am_item_lang');

if(class_exists('q2am_item_page'))
{
    //do the stuffs
}

if(class_exists('q2am_item_page') && class_exists('q2am_item_admin') ....)
{
    //do the stuffs
}

//May be we can get list of classes and check in_array

$classes = get_declared_classes();

if(in_array('q2am_item_page', $classes ))
{
    //do the stuffs
}

 

Find on Gist here https://gist.github.com/q2amarket/8951524

 


Edit: Once class confirmed than you can check function_exists() within the class

by
This seems to be one way to go. I will test it and report back :)

Thank you Jatin!
by
I have tested on other application I am developing and it works fine. So this should work on Q2A as well.

Waiting for your feedback..
by
just for clarification:
1. You are using this code above in your qa-plugin.php.
2. function get_declared_classes() is no core function but one of yours?
by
When I use this in qa-plugin.php I get the error: "Question2Answer fatal error: A event module named Edit History already exists. Please check there are no duplicate plugins. "

When I use this code directly in a plugin module, I get: "Question2Answer fatal error: qa_register_plugin_module() can only be called from a plugin qa-plugin.php file"

And when I directly try from within a plugin module:
    if(class_exists('qa_edit_history')) {
        $qa_content['custom'.++$c]='Plugin found';
    }
It does not show anything frontend.

:-(
by
//registering plugin stuffs are the function which you add into your qa-plugin.php than you will have respective php files containing registered classes.

Than you can check into theme file or in plugin layer or any php file if the class exists.
by
get_declared_classess() is php built-in function which list all classes in array format.
by
See my updated Gist code https://gist.github.com/q2amarket/8951524 go through comment sections
by
Okay, current situation:

I checked the declared classes (thanks for the tip btw):
    $classes = get_declared_classes();
    $qa_content['custom'.++$c]='<p>Classes: '.implode(', ', $classes).'</p>';

There I do not see the plugin that should actually be declared (in my case "qa_edit_history"). I see other plugins but not this one.

Could it be that we have again the problem here, that plugins are loaded one after each other and the *order* decides if we know about a plugin or not...
by
Why you are implode classes? that is only to check if is in array using in_array() function. You can check with class_exists() also. I have described different way, Whichever you prefer.

You can check with if(class_exists('qa_edit_history'){ //do stuffs } this also work and

$classes = get_declared_classes();
if(in_array('qa_edit_history', $classes){ //do stuffs } this too work both will check if class qa_edit_history exists or not.

Once you register your class than you can check if it is loaded or not for the page using print_r

$classes = get_declared_classes();
echo '<pre>', print_r($classes), '</pre>';

see if you are getting your class in this array
by
Instead of print_r I used implode which gives me the same data. And as i said, the other plugin module (qa_edit_history) is not listed in this array.

That is why  if(class_exists('qa_edit_history'){ //do stuffs }
does not work.

It seems that the edit-history-plugin is registered *after* the plugin where I am trying your code suggestion.

@Scott: I really thing we need a plugin_exist() core function here!
by
I guess this is exactly the reason why sama55 created a plugin:

"This plugin(required core hack) changes plugin execution order." → http://www.question2answer.org/qa/23788/new-plugin-plugin-order-required-core-hack?show=23788#q23788
by
I have already requested few important things.. plugin_exists(), functions and layer priority like wordpress add_action($priority); and few other stuffs. Without it definitely going to create lot of issue end of the day.

We are getting now more and more plugins, themes, hook and same method and functions been overridden again and again which creates conflict.

Hope things will be sorted out in next version. Meantime I will try to find some solution.. just need some free time..

BTW can you upload your code to github or may be on bitbucket creating private repo where I can check it?
...