# 1. Khi active plugin tự cài đặt options mặc định, thêm content vô bảng :) (ok)

Source tham khảo

{% file src="https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-Mci9NeCHfioY8XfU3eH%2F-Mci9vTA1R8Wz_cnZw04%2Fevergreen-content-poster.zip?alt=media&token=0eef7891-6f1a-4aeb-aaab-96bbc067b68e" %}

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MaWZ81PzCt02DxIv6R9%2F-MaWw4LLZ2Ne6lX74JXC%2Fimage.png?alt=media\&token=c00043ea-8502-4a1e-9ce3-553afffb21d3)

/var/www/reset/wp-content/plugins/lionel-setup-content/lionel\_setup\_content.php

```
<?php
/**
 *
 * @link              https://usergrowth.io/
 * @since             1.0.0
 * @package           Evergreen_Content_Poster
 *
 * @wordpress-plugin
 * Plugin Name:       Lionel Setup Content
 * Plugin URI:        https://usergrowth.io/resources/tools/evergreen-content-poster/?utm_source=plugin%20description&utm_medium=software&utm_campaign=Evergreen%20Content%20Poster
 * Description:       Automatically share your WordPress evergreen content on Twitter, Facebook, Instagram, LinkedIn or Pinterest. Get more visitors to your website and keep your content alive. For questions, comments, or feature requests, <a href="https://usergrowth.io/contact/?utm_source=plugin%20description&utm_medium=software&utm_campaign=Evergreen%20Content%20Poster">contact</a> us!
 * Version:           1.1.4
 * Author:            User Growth
 * Author URI:        https://usergrowth.io/?utm_source=plugin%20description&utm_medium=software&utm_campaign=Evergreen%20Content%20Poster
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       lionel
 * Domain Path:       /languages/
 */
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
  die;
}
  if ( ! defined ('ABSPATH')) {
    exit;
  }
  if ( ! defined ('LIONEL_CONTENT_PATH')) {
    define ('LIONEL_CONTENT_PATH',
      untrailingslashit (plugins_url ('', __FILE__)));
    //https://reset.com/wp-content/plugins/lionel-setup-content
  }
  if ( !defined('LIONEL_CONTENT_PLUGIN_DIR') )
    define('LIONEL_CONTENT_PLUGIN_DIR', untrailingslashit(dirname( __FILE__ )));
    // /var/www/reset/wp-content/plugins/lionel-setup-content
  if ( !defined('LIONEL_CONTENT_PLUGIN_SLUG') )
    define ('LIONEL_CONTENT_PLUGIN_SLUG', basename (dirname (__FILE__)));
//  lionel-setup-content
  
  if ( !defined('LIONEL_CONTENT_DB_VERSION') )
    define('LIONEL_CONTENT_DB_VERSION', '0.0.4');
  
  if ( !defined('LIONEL_CONTENT_PLUGIN_VERSION') )
    define('LIONEL_CONTENT_PLUGIN_VERSION', '1.1.4');
  
  if ( !defined('LIONEL_CONTENT_ENABLE_DEBUG') )
    define('LIONEL_CONTENT_ENABLE_DEBUG', TRUE);
  
  if ( !defined('LIONEL_CONTENT_KEY_PREFIX') )
    define('LIONEL_CONTENT_KEY_PREFIX', 'lionel');
  if( LIONEL_CONTENT_ENABLE_DEBUG ) {
    error_reporting (E_ALL);
    ini_set('display_errors', 1);
  }
  /**
   * Check if the DB version is older than the current one installed,
   * and run update scripts if needed
   */
  require_once plugin_dir_path (__FILE__)
    . 'includes/class-lionel_content-activator.php';
  register_activation_hook (__FILE__, 'activate_lionel_content');
  require plugin_dir_path (__FILE__) . 'includes/class-lionel_content.php';
  function run_lionel_content() {
    $plugin = new Lionel_Content();
    $plugin->run();
  }
  run_lionel_content();
```

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MaWYnVEvaCkZGTwoL8U%2F-MaWZ0bE8Q2vGLNJgjcD%2Fimage.png?alt=media\&token=f8875895-2e4c-4858-b86b-210739572065)

/var/www/reset/wp-content/plugins/lionel-setup-content/includes/class-lionel\_content.php

```
<?php
  class Lionel_Content
  {
    protected $loader;
    protected $plugin_name;
    protected $version;
    private $plugin_admin;
    private $buffer_service;
    public function __construct ()
    {
      if ( defined( 'LIONEL_CONTENT_PLUGIN_VERSION' ) ) {
        $this->version = LIONEL_CONTENT_PLUGIN_VERSION;
      } else {
        $this->version = '1.0.0';
      }
      $this->plugin_name = 'lionel_content';
      $this->load_dependencies();
    }
    private function load_dependencies ()
    {
      require_once plugin_dir_path (dirname (__FILE__))
        . 'includes/class-lionel_content-loader.php';
      $this->loader = new Lionel_Content_Loader();
    }
    public function get_loader() {
      return $this->loader;
    }
    public function get_plugin_name() {
      return $this->plugin_name;
    }
    public function get_version() {
      return $this->version;
    }
    public static $instance;
    public static function get_instance() {
      if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) {
        self::$instance = new self;
      }
      return self::$instance;
    }
    public function install() {
      $this->add_lionel_content_defaults();
    }
    public function add_lionel_content_defaults ()
    {
      global $wpdb;
      $options = get_option ('lionel_options');
      if ( !isset($options['lionel_installed']) || $options['lionel_installed'] != 1 || !is_array($options) )
      {
        $opt = array(
          'lionel_version' => LIONEL_CONTENT_PLUGIN_VERSION,
          'lionel_db_version' => LIONEL_CONTENT_DB_VERSION,
          'lionel_installed' => 1,
          'premium' => 0
        );
        if ( is_multisite() )
        {
          update_site_option( 'lionel_options', $opt );
        } else {
          $this->lionel_content_update_option( 'lionel_options', $opt );
        }
      }
      $this->lionel_content_db_install();
    }
    public function lionel_content_update_option( $option_name, $new_value ) {
      if ( get_option( $option_name ) !== false ) {
        // The option already exists, so we just update it.
        update_option( $option_name, $new_value );
      } else {
        // The option hasn't been added yet. We'll add it with $autoload set to 'no'.
        add_option( $option_name, $new_value, $deprecated=null, $autoload='no' );
      }
    }
    public function lionel_content_db_install ()
    {
      global $wpdb;
      $charset_collate = $wpdb->get_charset_collate ();
      $this->lionel_content_set_wpdb_tables ();
      require_once (ABSPATH . 'wp-admin/includes/upgrade.php');
      $sql = "CREATE TABLE IF NOT EXISTS $wpdb->lionel_content_logs (
        `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
        `post_id` int(11) NOT NULL,
        `action` enum('publish','update','repost','bulk_publish', 'share', 'add_queue') DEFAULT NULL,
        `request_sent` datetime NOT NULL,
        `profile_id` varchar(191) NOT NULL,
        `profile_name` varchar(191) NOT NULL DEFAULT '',
        `result` enum('success','test','pending','warning','error') NOT NULL DEFAULT 'success',
        `result_message` text,
        `status_text` text,
        `status_image` text,
        `status_link` text,
        `status_created_at` datetime DEFAULT NULL,
        `status_due_at` datetime DEFAULT NULL,
        `lionel_posting_schedule` text,
        PRIMARY KEY (`id`),
        KEY `post_id` (`post_id`),
        KEY `action` (`action`),
        KEY `result` (`result`),
        KEY `profile_id` (`profile_id`)
      ) {$charset_collate};";
      dbDelta ($sql);
      $sqlCate = "CREATE TABLE IF NOT EXISTS $wpdb->lionel_categories (
        `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
        `name` varchar(191) NOT NULL DEFAULT '',
        `description` text,
        `random_availability` int(1) NOT NULL DEFAULT 1,
        `status` int(1) NOT NULL DEFAULT 1,
        `is_default` int(1) DEFAULT NULL,
        `created_at` datetime DEFAULT NULL,
        `updated_at` datetime DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) {$charset_collate};";
      dbDelta ($sqlCate);
      // Check categories
      $categories = $wpdb->get_results("SELECT * FROM {$wpdb->lionel_categories}");
      if(empty((array)$categories)) {
        // add default Category
        $dateNow = date('Y-m-d H:i:s');
        $insertSql = "INSERT INTO {$wpdb->prefix}lionel_categories (name, description, random_availability, status, is_default, created_at, updated_at) VALUES ('Your evergreen content', 'Use this category to promote your own content - share any blog posts or pages you publish.', 1, 1, 1, '$dateNow', null), ('Promotional', 'Use this category to promote your services/products. Make sure to include Calls To Actions (CTAs) like \"Book a call with us\" or \"start a trial account\".', 1, 1, 0, '$dateNow', null), ('Quotes, Questions, and Fun', 'Use this category to generate engagement on your socials. Share inspirational or fun quotes, from your niche or create open questions for your audience.', 1, 1, 0, '$dateNow', null)";
        dbDelta($insertSql);
      }
    }
    public function lionel_content_set_wpdb_tables()
    {
      global $wpdb;
      $wpdb->lionel_content_logs = $wpdb->prefix . 'lionel_content_logs';
      $wpdb->lionel_categories = $wpdb->prefix . 'lionel_categories';
    }
    public function run ()
    {
      $this->loader->run();
    }
  }
```

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MaXLnvGLVXAWxeq4DUi%2F-MaXMAFEGZ5JW2j86g5Y%2Fimage.png?alt=media\&token=0ad026f5-3699-4d2d-8426-d188eae89544)

/var/www/reset/wp-content/plugins/lionel-setup-content/includes/class-lionel\_content-activator.php

```
<?php
  class Lionel_Content_Activator
  {
    public static function lionel_activate( $network_wide ) {
      if ( ! current_user_can( 'activate_plugins' ) )
        return;
      // Initialise Plugin
      $lionel = Lionel_Content::get_instance();
      // Check if we are on a multisite install, activating network wide, or a single install
      if ( ! is_multisite() || ! $network_wide ) {
        // Single Site activation
        // $ecp->get_class( 'install' )->install();
        $lionel->install ();
      } else      // Multisite network wide activation
        $sites = get_sites( array('number' => 0) );
        foreach ( $sites as $site ) {
          switch_to_blog( $site->blog_id );
          $lionel = (new Lionel_Content)->install();
          restore_current_blog();
        }
      }
    public static function register_settings ()
    {
      $opts = array(
        'lionel_version' => LIONEL_CONTENT_PLUGIN_VERSION,
        'lionel_db_version' => LIONEL_CONTENT_DB_VERSION,
        'premium' => 0
      );
      add_option (LIONEL_CONTENT_KEY_PREFIX . '_options', $opts);
      add_option( LIONEL_CONTENT_KEY_PREFIX . '_general_settings', [] );
    }
  }
  function activate_lionel_content ($network_wide)
  {
    Lionel_Content_Activator::lionel_activate ($network_wide);
    Lionel_Content_Activator::register_settings();
  }
```

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MaXNpg-I0vS6G_SjdW6%2F-MaXOUy7uPKCM4VXYT1k%2Fimage.png?alt=media\&token=93c64197-7cd9-4e03-ba28-57ba9bf596ab)

/var/www/reset/wp-content/plugins/lionel-setup-content/includes/class-lionel\_content-loader.php

```
<?php
  class Lionel_Content_Loader
  {
    protected $actions;
    protected $filters;
    public function __construct() {
      $this->actions = array();
      $this->filters = array();
    }
    public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
      $this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
    }
    public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
      $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
    }
    private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) {
      $hooks[] = array(
        'hook'          => $hook,
        'component'     => $component,
        'callback'      => $callback,
        'priority'      => $priority,
        'accepted_args' => $accepted_args
      );
      return $hooks;
    }
    public function run() {
      foreach ( $this->filters as $hook ) {
        add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
      }
      foreach ( $this->actions as $hook ) {
        add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
      }
    }
  }
```

Full code :)

/var/www/reset/wp-content/plugins

{% file src="https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MaXPDhkYNCgRuuoY_7_%2F-MaXPr1OOZMJxpHkOOH1%2Flionel-setup-content.zip?alt=media&token=d820f56f-08e5-44ff-92f4-6a4b6d2abf97" %}
