Last active
October 9, 2024 00:34
-
-
Save sohan5005/258bc7c8cc9e942efa8af0be38323f21 to your computer and use it in GitHub Desktop.
A class that can can be used to have some default sections to King Composer for WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if( class_exists( 'KC_Section_Importer' ) ) { | |
return; | |
} | |
class KC_Section_Importer { | |
/** | |
* Define the post_type & taxonomy to use | |
*/ | |
public $taxonomy = 'kc-section-category'; | |
public $type = 'kc-section'; | |
/** | |
* Instance factory | |
*/ | |
public static $_instance; | |
public static function getInstance() { | |
if( !( self::$_instance instanceof self ) ) { | |
self::$_instance = new self; | |
} | |
return self::$_instance; | |
} | |
/** | |
* constructor | |
*/ | |
public function __construct() { | |
} | |
/** | |
* the importer | |
*/ | |
public function import() { | |
if( !post_type_exists('kc-section') ) { | |
return; | |
} | |
$template = require_once plugin_dir_path( __FILE__ ) . 'template.php'; | |
foreach( $template as $cat_name => $sections ) { | |
$cat_slug = $this->makeslug( $cat_name ); | |
$term = get_term_by( 'slug', $cat_slug, $this->taxonomy ); | |
if( $term === false ) { | |
$term = wp_insert_term( $cat_name, $this->taxonomy, array( | |
'slug' => $cat_slug, | |
) ); | |
$term_id = $term['term_id']; | |
} else { | |
$term_id = (int)$term->term_id; | |
} | |
foreach( $sections as $section ) { | |
$section_name = $section['title']; | |
$post = get_page_by_title( $section_name, 'OBJECT', $this->type ); | |
if( is_null( $post ) ) { | |
$post = array( | |
'post_title' => $section_name, | |
'post_content' => $section['content'], | |
'post_status' => 'publish', | |
'post_type' => $this->type, | |
'tax_input' => array( | |
$this->taxonomy => array( $term_id ), | |
), | |
'meta_input' => array( | |
'kc_data' => array( | |
'mode' => 'kc', | |
'classes' => '', | |
'max_width' => '', | |
'thumbnail' => esc_url( $section['thumbnail'] ), | |
'css' => '', | |
), | |
), | |
); | |
$id = wp_insert_post( $post ); | |
} | |
} | |
} | |
} | |
/** | |
* Converts a text to slug | |
*/ | |
public static function makeslug( $text ) { | |
$text = preg_replace( '~[^\pL\d]+~u', '-', $text ); | |
if( function_exists( 'iconv' ) ) { | |
$text = iconv( 'utf-8', 'us-ascii//TRANSLIT', $text ); | |
} | |
$text = preg_replace( '~[^-\w]+~', '', $text ); | |
$text = trim( $text, '-' ); | |
$text = preg_replace( '~-+~', '-', $text ); | |
$text = strtolower( $text ); | |
return $text; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The function is hooked on admin_init | |
* | |
* That means these sections cannot be removed. If someone deletes any of these sections, they will be added automatically again | |
* | |
* You can modify them to run at activation only | |
*/ | |
add_action( 'admin_init', 'myplugin_add_sections' ); | |
function myplugin_add_sections() { | |
require_once plugin_dir_path( __FILE__ ) . 'class-kc-section-importer.php'; | |
$importer = KC_Section_Importer::init(); | |
$importer->import(); | |
return; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This file should return array of sections | |
*/ | |
return array( | |
// array of category | |
'My Category' => array( | |
array( | |
'title' => __( 'Hero section', 'textdomain' ), // Title | |
'thumbnail' => plugin_dir_url( __FILE__ ) . 'thumbs/hero.jpg', // Thumbnail file | |
'content' => '[kc_row _id="923527" use_container="no"][my_hero_section][/kc_column][/kc_row]', // the content of section | |
), | |
// .. more section | |
), | |
// .. more categories | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah! Awesome Sohan,
Welcome you to KingComposer page builder world