Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2008
    Location
    Croatia
    Posts
    1,744
    Plugin Contributions
    22

    Default Encapsulated Plugins - frontend is_enabled() check

    Unless I'm missing something obvious, it seems like there's no frontend function available to check if a certain plugin is installed and enabled?

    I have built an Encapsulated Plugin that runs on both frontend and backend (Testimonials). Everything is fine, but I'd like to add a "centerbox" to the home page with some testimonials displayed there. Since there are no hooks to attach it to (@drbyte looking at you - remember the solution I had presented?), I'll just modify the template file. However, I'd like it to be dynamic and displayed only if the plugin is installed and enabled. In case the plugin gets disabled, the centerbox would need to be disabled as well. Turns out there's no function available (at least to my knowledge) that would allow me to check if a certain plugin is installed/enabled or not.

    Am I missing something here? If not, I'm happy to PR:
    Code:
    function is_zc_plugin_enabled($plugin_key) {
        global $db;
        
        // Querying the correct plugin_control table
        $sql = "SELECT status 
                FROM " . TABLE_PLUGIN_CONTROL . " 
                WHERE unique_key = :pluginKey: 
                LIMIT 1";
                
        $sql = $db->bindVars($sql, ':pluginKey:', $plugin_key, 'string');
        $check_plugin = $db->Execute($sql);
    
        // If the record exists AND its status is 1, it is installed and enabled
        if ($check_plugin->RecordCount() > 0 && (int)$check_plugin->fields['status'] === 1) {
            return true;
        }
        
        return false;
    }

  2. #2
    Join Date
    Jan 2004
    Posts
    66,446
    Plugin Contributions
    81

    Default Re: Encapsulated Plugins - frontend is_enabled() check

    I'm AFK just now, but I remember doing front-end things with the MAP Pricing encapsulated plugin. That's where I'd look first. Might even just be the presence of a config key being defined.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  3. #3
    Join Date
    Oct 2008
    Location
    Croatia
    Posts
    1,744
    Plugin Contributions
    22

    Default Re: Encapsulated Plugins - frontend is_enabled() check

    I *was* thinking of using a constant, but my plugin has no constants added to config. Besides, what's the point of adding a single config setting to enable/disable a plugin when you have that feature in Plugin Manager that allows you to disable a plugin? Imagine the confusion when a user disables the plugin in Plugin Manager, but the constant MY_PLUGIN_ENABLED is still set to true and we're relying on that constant.

    The only thing I probably could hook onto is one single function and use function_exists, but I'm not too thrilled with the idea, especially not as a long-term solution in general. Also not 100% sure if it will exist in frontend when a plugin is disabled (not un-installed) - probably worth testing to find out. What if a plugin doesn't have any custom functions or custom classes?
    I could also use the $sniffer->table_exists(), but that doesn't tell me if the plugin is disabled or not.

  4. #4
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,965
    Plugin Contributions
    96

    Default Re: Encapsulated Plugins - frontend is_enabled() check

    You could have an auto-loaded observer as part of the encapsulated plugin's file-set and code that observer such that it sets a 'global' variable that indicates its presence.

    The code that's deciding whether/not to include that centerbox could then simply check for !empty($plugin_presence_indicator) to do its thing.

  5. #5
    Join Date
    Apr 2009
    Posts
    496
    Plugin Contributions
    2

    Default Re: Encapsulated Plugins - frontend is_enabled() check

    in the up coming release zc 2.2 there is the getInstalledPlugins method in includes/classes/DbRepositories/PluginControlRepository.php which would give you a list of installed plugins that are enabled. If you were to add a check function I would suggest that that is where it needs to go.
    Mark Brittain
    http:\\innerlightcrystals.co.uk\sales\

  6. #6
    Join Date
    Oct 2008
    Location
    Croatia
    Posts
    1,744
    Plugin Contributions
    22

    Default Re: Encapsulated Plugins - frontend is_enabled() check

    Quote Originally Posted by lat9 View Post
    You could have an auto-loaded observer as part of the encapsulated plugin's file-set and code that observer such that it sets a 'global' variable that indicates its presence.

    The code that's deciding whether/not to include that centerbox could then simply check for !empty($plugin_presence_indicator) to do its thing.
    Damn it, Cindy, that was a brilliant idea - I would never have thought of that! Many thanks!

    For anyone who would like to do the same, here's the solution:
    Add a new file in your zc_plugins/your_plugin/version/catalog/includes/classes/observers/ named auto.yourplugin_plugin_presence_indicator.php (in my case auto.testimonials_plugin_presence_indicator) with the following content:
    Code:
    <?php
    /**
     * Observer class used to indicate the plugin is active
     */
    class zcObserverTestimonialsPluginPresenceIndicator extends base
    {
        public function __construct()
        {
            // Declare the global variable and set it to true
            global $testimonials_plugin_presence_indicator;
            $testimonials_plugin_presence_indicator = true;
        }
    }
    Don't forget to change the class name based on file name.

    That's it - now you have a global variable you can use anywhere in the system. In my case, I'm using it like this:
    Code:
    <?php
    if (!empty($testimonials_plugin_presence_indicator)) {
        require($template->get_template_dir('tpl_modules_testimonials.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_testimonials.php');
    }
    ?>

 

 

Similar Threads

  1. v210 jscript_*.php in encapsulated plugins
    By cstdenis2 in forum Bug Reports
    Replies: 2
    Last Post: 3 Aug 2025, 04:15 PM
  2. v210 Encapsulated Plugins
    By Congerman in forum General Questions
    Replies: 5
    Last Post: 7 Apr 2025, 08:41 PM
  3. Encapsulated Plugins - Payment modules
    By niccol in forum Code Collaboration
    Replies: 2
    Last Post: 2 Feb 2022, 09:47 PM
  4. v157 Encapsulated Plugins
    By niccol in forum Bug Reports
    Replies: 6
    Last Post: 22 Jul 2021, 05:53 PM
  5. v157 encapsulated plugins require question....
    By carlwhat in forum All Other Contributions/Addons
    Replies: 19
    Last Post: 13 Oct 2020, 12:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg