使用CTIVATED_Plugin运行代码

时间:2016-05-15 作者:Naz

我正在使用Site Originates Page Builder插件,并希望在安装主题时安装该插件,因此我正在使用TGMPluginActivation。这可以正确地安装和激活一组插件。然而,对于页面生成器及其免费插件小部件包,我想在插件激活时设置一些默认值,例如确保所有小部件都已安装,对于页面生成器,确保打开Tablet responsive并设置正确的断点。

我使用的代码是

<?php
/*
     ============================================
        Page Builder and Widgets Integration
     ============================================
*/ 

if (!function_exists(\'activateAllSiteOriginWidgets\')):
    function activateAllSiteOriginWidgets()
    {
        global $wpdb;
        $table_name = $wpdb->prefix . "options";

        $sow_button = array(\'widget_sow-button\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');
        $sow_google_map = array(\'widget_sow-google-map\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');
        $sow_image = array(\'widget_sow-image\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');
        $sow_slider = array(\'widget_sow-slider\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');
        $sow_post_carusel = array(\'widget_sow-post-carousel\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');
        $sow_editor = array(\'widget_sow-editor\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');
        $widgets_active = array(\'siteorigin_widgets_active\', \'a:17:{s:6:"button";b:1;s:10:"google-map";b:1;s:5:"image";b:1;s:6:"slider";b:1;s:13:"post-carousel";b:1;s:6:"editor";b:1;s:3:"cta";b:1;s:8:"features";b:1;s:7:"contact";b:1;s:8:"headline";b:1;s:4:"hero";b:1;s:10:"image-grid";b:1;s:20:"social-media-buttons";b:1;s:11:"testimonial";b:1;s:14:"simple-masonry";b:1;s:11:"price-table";b:1;s:5:"video";b:1;}\', \'yes\');
        $sow_cta = array(\'widget_sow-cta\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');
        $sow_features = array(\'widget_sow-features\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');
        $contact_form = array(\'widget_sow-contact-form\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');
        $sow_headline = array(\'widget_sow-headline\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');
        $sow_hero = array(\'widget_sow-hero\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');
        $image_grid = array(\'widget_sow-image-grid\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');
        $social_media = array(\'widget_sow-social-media-buttons\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');
        $sow_testimonials = array(\'widget_sow-testimonials\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');
        $sow_simple_masonry = array(\'widget_sow-simple-masonry\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');
        $price_table = array(\'widget_sow-price-table\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');
        $show_wideo = array(\'widget_sow-video\', \'a:1:{s:12:"_multiwidget";i:1;}\', \'yes\');


        $options_array = array($sow_button, $sow_google_map, $sow_image, $sow_slider, $sow_post_carusel, $sow_editor, $widgets_active, $sow_cta, $sow_features, $contact_form, $sow_headline, $sow_hero, $image_grid, $social_media, $sow_testimonials, $sow_simple_masonry, $price_table, $show_wideo);
        foreach ($options_array as $option) {
            $option_name = $option[0];
            $option_content = $option[1];
            $option_active = $option[2];
            $data = array(\'option_name\' => $option_name, \'option_value\' => $option_content, \'autoload\' => $option_active);
            $sql = "SELECT `option_name` FROM \'$table_name\' WHERE `option_name` = \'$option_name\'";
            $check_option = $wpdb->get_results($sql);
            if ($check_option) {
                $where = array(\'option_name\' => $option_name);
                $wpdb->update($table_name, $data, $where);
            } else {
                $wpdb->insert($table_name, $data);
            }
        }
        //activate panels directory for current user
        $user = get_current_user_id();
        update_user_meta( $user, \'so_panels_directory_enabled\', true );

//--------------THIS IS THE PART THAT DOESN\'T RUN
        // Register SiteOrigin options, make the tablet layout acticve and set the breaks points to 991 for tables and 480 for mobile to match Bootstrap
        $SiteOriginOptions = \'a:14:{s:10:"post-types";a:2:{i:0;s:4:"page";i:1;s:4:"post";}s:10:"title-html";s:39:"<h3 class="widget-title">{{title}}</h3>";s:16:"add-widget-class";b:1;s:15:"bundled-widgets";b:0;s:19:"recommended-widgets";b:1;s:10:"responsive";b:1;s:13:"tablet-layout";b:1;s:12:"tablet-width";i:991;s:12:"mobile-width";i:480;s:13:"margin-bottom";i:30;s:22:"margin-bottom-last-row";b:0;s:12:"margin-sides";i:30;s:20:"full-width-container";s:4:"body";s:12:"copy-content";b:1;}\';
        $data_options = array(\'option_name\' => \'siteorigin_panels_settings\', \'option_value\' => $SiteOriginOptions, \'autoload\' => \'yes\');
        $where_options = array(\'option_name\' => \'siteorigin_panels_settings\');
        $wpdb->update($table_name, $data_options, $where_options);


    }
endif;

add_action( \'activated_plugin\', \'activateAllSiteOriginWidgets\' );


?>
对于小部件来说,代码工作正常,对于页面生成器来说,当TGMPA安装并激活插件时,它似乎不会运行代码,但是,如果我切换主题或取消激活,激活插件,那么它就工作了。

我所拥有的代码的TGMPA部分是;

array(
    \'name\'               => \'Site Origins Page Builder\', 
    \'slug\'               => \'siteorigin-panels\', 
    \'source\'             => get_template_directory() . \'/plugins/so-page-builder.zip\',
    \'required\'           => true,
    \'version\'            => \'\',
    \'force_activation\'   => true,
    \'force_deactivation\' => true,
    \'external_url\'       => \'\',
    \'is_callable\'        => \'\',
),

array(
    \'name\'               => \'Site Origins Widgets Bundle\', 
    \'slug\'               => \'so-widgets-bundle\', 
    \'source\'             => get_template_directory() . \'/plugins/so-widgets-bundle.zip\',
    \'required\'           => true,
    \'version\'            => \'\',
    \'force_activation\'   => true,
    \'force_deactivation\' => true,
    \'external_url\'       => \'\',
    \'is_callable\'        => \'\',
),
你知道为什么widget会更新代码而不是页面生成器代码吗?

1 个回复
SO网友:majick

为什么不简单地尝试一下:

$SiteOriginOptions = \'a:14:{s:10:"post-types";a:2:{i:0;s:4:"page";i:1;s:4:"post";}s:10:"title-html";s:39:"<h3 class="widget-title">{{title}}</h3>";s:16:"add-widget-class";b:1;s:15:"bundled-widgets";b:0;s:19:"recommended-widgets";b:1;s:10:"responsive";b:1;s:13:"tablet-layout";b:1;s:12:"tablet-width";i:991;s:12:"mobile-width";i:480;s:13:"margin-bottom";i:30;s:22:"margin-bottom-last-row";b:0;s:12:"margin-sides";i:30;s:20:"full-width-container";s:4:"body";s:12:"copy-content";b:1;}\';
update_option(\'siteorigin_panels_settings\', $SiteOriginOption);
此外,您可能希望设置一个标志,表示选项已启动,以便用户稍后可以更改它们,并且在激活插件时不会覆盖它们。