2. load library admin css, js (ok)
Load file js
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 ();
$this->define_admin_hooks();
}
private function load_dependencies ()
{
require_once plugin_dir_path (dirname (__FILE__))
. 'includes/class-lionel_content-loader.php';
$this->loader = new Lionel_Content_Loader();
/**
* Part 3
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-lionel_content-admin.php';
}
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();
}
/**
* Part 4
*/
private function define_admin_hooks ()
{
$plugin_admin = new Lionel_Content_Admin($this->get_plugin_name (),
$this->get_version ());
$this->plugin_admin = $plugin_admin;
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
}
}
wp-content/plugins/lionel-setup-content/admin/class-lionel_content-admin.php
<?php
class Lionel_Content_Admin
{
private $plugin_name;
private $version;
private $plugin_public_name;
private $service;
private $client_id;
private $settings;
private $buffer_service;
public function __construct ($plugin_name, $version)
{
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->plugin_public_name = 'Lionel Content';
$this->service = 'Buffer';
}
public function enqueue_scripts ()
{
if (LIONEL_CONTENT_ENABLE_DEBUG == true) :
wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/lionel_content-admin.js', array('jquery'), time(), false);
// set variables for script
wp_localize_script($this->plugin_name, 'ajaxSettings', array('ajaxurl' => admin_url('admin-ajax.php')));
else :
wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/lionel_content-admin.js', array('jquery'), $this->version, false);
// set variables for script
wp_localize_script($this->plugin_name, 'ajaxSettings', array('ajaxurl' => admin_url('admin-ajax.php')));
endif;
}
}
Thành qủa là đây :)
Full code
2. Load file css
wp-content/plugins/lionel-setup-content/admin/class-lionel_content-admin.php
<?php
class Lionel_Content_Admin
{
private $plugin_name;
private $version;
private $plugin_public_name;
private $service;
private $client_id;
private $settings;
private $buffer_service;
public function __construct ($plugin_name, $version)
{
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->plugin_public_name = 'Lionel Content';
$this->service = 'Buffer';
$this->load_dependencies();
}
public function enqueue_styles ()
{
if (LIONEL_CONTENT_ENABLE_DEBUG == true) :
wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/lionel_content-admin.css', array(), time(), 'all');
else :
wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/lionel_content-admin.css', array(), $this->version, 'all');
endif;
}
public function enqueue_scripts ()
{
if (LIONEL_CONTENT_ENABLE_DEBUG == true) :
wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/lionel_content-admin.js', array('jquery'), time(), false);
// set variables for script
wp_localize_script($this->plugin_name, 'ajaxSettings', array('ajaxurl' => admin_url('admin-ajax.php')));
else :
wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/lionel_content-admin.js', array('jquery'), $this->version, false);
// set variables for script
wp_localize_script($this->plugin_name, 'ajaxSettings', array('ajaxurl' => admin_url('admin-ajax.php')));
endif;
}
public function load_dependencies ()
{
require_once plugin_dir_path(dirname(__FILE__)) . 'admin/class-lionel_content-settings.php';
}
}
wp-content/plugins/lionel-setup-content/admin/css/lionel_content-admin.scss
body {
color: red !important;
}
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 ();
$this->define_admin_hooks();
}
private function load_dependencies ()
{
require_once plugin_dir_path (dirname (__FILE__))
. 'includes/class-lionel_content-loader.php';
$this->loader = new Lionel_Content_Loader();
/**
* Part 3
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-lionel_content-admin.php';
}
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();
}
/**
* Part 4
*/
private function define_admin_hooks ()
{
$plugin_admin = new Lionel_Content_Admin($this->get_plugin_name (),
$this->get_version ());
$this->plugin_admin = $plugin_admin;
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
}
}
Previous1. Khi active plugin tự cài đặt options mặc định, thêm content vô bảng :) (ok)Next3. Load page setting admin (ok)
Last updated