first commit

This commit is contained in:
CHIEFSOFT\ameye
2023-11-30 13:20:54 -05:00
commit e9e5c0546c
5833 changed files with 1801865 additions and 0 deletions
@@ -0,0 +1,88 @@
<?php
/**
* Deprecated features and migration functions
*
* @package Powerkit
* @subpackage Core
*/
/**
* Migration to 2.0.7
*
* @param string $current Current version.
* @param string $new New version.
*/
add_action( 'powerkit_plugin_upgrade', function ( $current, $new ) {
if ( version_compare( $current, '2.0.7', '<' ) ) {
require_once POWERKIT_PATH . 'modules/post-views/helpers/db-powerkit-post-views.php';
$post_views_db = new Powerkit_Post_Views_DB();
$post_views_db->activate_single();
}
}, 10, 2 );
/**
* Migration to 2.2.2
*
* @param string $current Current version.
* @param string $new New version.
*/
add_action( 'powerkit_plugin_upgrade', function ( $current, $new ) {
if ( version_compare( $current, '2.2.2', '<' ) ) {
$transients = array(
'powerkit_connect_instagram_app_access_token',
'powerkit_connect_instagram_app_type',
'powerkit_connect_instagram_app_user_id',
'powerkit_connect_instagram_app_username',
'powerkit_connect_instagram_app_refresh',
'powerkit_connect_facebook_app_access_token',
'powerkit_connect_facebook_app_accounts',
'powerkit_connect_facebook_app_refresh',
'powerkit_connect_twitter_app_user_id',
'powerkit_connect_twitter_app_screen_name',
'powerkit_connect_twitter_app_oauth_token',
'powerkit_connect_twitter_app_oauth_token_secret',
);
foreach ( $transients as $key => $transient ) {
if ( get_transient( $transient ) && ! get_option( $transient ) ) {
update_option( $transient, get_transient( $transient ) );
}
}
if ( ! wp_next_scheduled( 'event_access_token_refresh' ) ) {
wp_schedule_event( time(), 'daily', 'event_access_token_refresh' );
}
}
}, 10, 2 );
/**
* Migration to 2.3.0
*
* @param string $current Current version.
* @param string $new New version.
*/
add_action( 'powerkit_plugin_upgrade', function ( $current, $new ) {
if ( version_compare( $current, '2.3.0', '<' ) ) {
if ( 'before' === get_option( 'powerkit_toc_display', 'none' ) ) {
update_option( 'powerkit_toc_enable_automatically', true );
}
if ( 'after' === get_option( 'powerkit_toc_display', 'none' ) ) {
update_option( 'powerkit_toc_enable_automatically', true );
}
}
}, 10, 2 );
/**
* Migration to 2.6.2
*
* @param string $current Current version.
* @param string $new New version.
*/
add_action( 'powerkit_plugin_upgrade', function ( $current, $new ) {
if ( version_compare( $current, '2.6.2', '<' ) && get_option( 'powerkit_db_version' ) ) {
update_option( 'powerkit_enabled_headers_footers', 1 );
}
}, 10, 2 );
@@ -0,0 +1,64 @@
<?php
/**
* Module admin carcase
*
* @package Powerkit
* @subpackage Core
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Module admin class.
*/
class Powerkit_Module_Admin {
/**
* The module slug.
*
* @var string $slug The module slug.
*/
public $slug = null;
/**
* __construct
*
* This function will initialize the initialize
*
* @param string $slug The module slug.
*/
public function __construct( $slug = null ) {
// Init slug of module.
$this->slug = $slug;
// Initialize.
$this->initialize();
// Actions.
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
}
/**
* Initialize
*
* This function will initialize the module
*/
public function initialize() {
/* do nothing */
}
/**
* Load the required dependencies for this module.
*
* @param string $page Current page.
*/
public function admin_enqueue_scripts( $page ) {
/* do nothing */
}
}
@@ -0,0 +1,71 @@
<?php
/**
* Module public carcase
*
* @package Powerkit
* @subpackage Core
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Module public class.
*/
class Powerkit_Module_Public {
/**
* The module slug.
*
* @var string $slug The module slug.
*/
public $slug = null;
/**
* __construct
*
* This function will initialize the initialize
*
* @param string $slug The module slug.
*/
public function __construct( $slug = null ) {
// Init slug of module.
$this->slug = $slug;
// Initialize.
$this->initialize();
// Actions.
add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) );
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
}
/**
* Initialize
*
* This function will initialize the module
*/
public function initialize() {
/* do nothing */
}
/**
* Load the required dependencies for this module.
*/
public function wp_enqueue_scripts() {
/* do nothing */
}
/**
* Load the required dependencies for this module in block editr.
*/
public function enqueue_block_editor_assets() {
/* do nothing */
}
}
@@ -0,0 +1,149 @@
<?php
/**
* Module carcase
*
* @package Powerkit
* @subpackage Core
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Module main class.
*/
class Powerkit_Module {
/**
* The module name.
*
* @var string $name The module name.
*/
public $name = null;
/**
* The module description.
*
* @var string $desc The module description.
*/
public $desc = null;
/**
* The module slug.
*
* @var string $slug The module slug.
*/
public $slug = null; // Required.
/**
* The module category.
*
* @var string $category The module category.
*/
public $category = 'basic';
/**
* The module priority.
*
* @var string $priority The module priority.
*/
public $priority = 9999;
/**
* The module type.
*
* @var string $type The module type.
*/
public $type = 'default';
/**
* The module public side of the site.
*
* @var string $category The module public side of the site.
*/
public $public = true;
/**
* The module enabled.
*
* @var string $enabled The module enabled.
*/
public $enabled = true;
/**
* The module badge.
*
* @var string $badge The module badge.
*/
public $badge = null;
/**
* The module load extensions.
*
* @var string $load_extensions The module load extensions.
*/
public $load_extensions = array();
/**
* The module actions links.
*
* @var string $links The module actions links.
*/
public $links = array();
/**
* __construct
*
* This function will initialize the initialize
*/
public function __construct() {
// Initialize.
$this->register();
// Register module info.
powerkit_register_module_info( array(
'name' => $this->name,
'desc' => $this->desc,
'slug' => $this->slug,
'type' => $this->type,
'category' => $this->category,
'priority' => $this->priority,
'public' => $this->public,
'enabled' => $this->enabled,
'badge' => $this->badge,
'load_extensions' => $this->load_extensions,
'links' => $this->links,
) );
// Check enabled module.
if ( ! powerkit_module_enabled( $this->slug ) ) {
return false;
}
// Define the functionality of the module.
$this->initialize();
}
/**
* Register
*
* This function will register the module
*/
public function register() {
/* do nothing */
}
/**
* Initialize
*
* This function will initialize the module
*/
public function initialize() {
/* do nothing */
}
}
@@ -0,0 +1,370 @@
<?php
/**
* The file that defines the core plugin class
*
* A class definition that includes attributes and functions used across both the
* public-facing side of the site and the admin area.
*
* @package Powerkit
* @subpackage Core
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( ! class_exists( 'Powerkit' ) ) {
/**
* Main Core Class
*/
class Powerkit {
/**
* The plugin version number.
*
* @var string $data The plugin version number.
*/
public $version;
/**
* The plugin data array.
*
* @var array $data The plugin data array.
*/
public $data = array();
/**
* The plugin settings array.
*
* @var array $settings The plugin data array.
*/
public $settings = array();
/**
* INIT (global theme queue)
*/
public function init() {
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
// Get plugin data.
$plugin_data = get_plugin_data( POWERKIT_PATH . '/powerkit.php' );
// Vars.
$this->version = $plugin_data['Version'];
// Settings.
$this->settings = array(
'name' => esc_html__( 'Powerkit', 'powerkit' ),
'version' => $plugin_data['Version'],
'documentation' => $plugin_data['AuthorURI'] . '/documentation/powerkit',
);
// Constants.
$this->define( 'POWERKIT', true );
$this->define( 'POWERKIT_VERSION', $plugin_data['Version'] );
// Include core.
require_once POWERKIT_PATH . 'core/core-powerkit-api.php';
require_once POWERKIT_PATH . 'core/core-powerkit-functions.php';
require_once POWERKIT_PATH . 'core/core-powerkit-helpers.php';
require_once POWERKIT_PATH . 'core/core-powerkit-post-meta.php';
// Include core classes.
require_once POWERKIT_PATH . 'core/class-powerkit-module.php';
require_once POWERKIT_PATH . 'core/class-powerkit-module-admin.php';
require_once POWERKIT_PATH . 'core/class-powerkit-module-public.php';
require_once POWERKIT_PATH . 'core/class-powerkit-deprecated.php';
// Include admin classes.
require_once POWERKIT_PATH . 'admin/class-admin-powerkit-manager.php';
// Include modules.
powerkit_load_files( 'modules' );
// Include extensions.
powerkit_load_files( 'extensions' );
// Actions.
add_action( 'powerkit_plugin_activation', array( $this, 'activation' ) );
add_action( 'plugins_loaded', array( $this, 'check_version' ) );
add_action( 'amp_post_template_css', array( $this, 'amp_enqueue_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ), 5 );
add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ), 5 );
add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
add_filter( 'style_loader_tag', array( $this, 'style_loader_tag' ), 10, 2 );
add_filter( 'wp_kses_allowed_html', array( $this, 'wp_kses_allowed_html' ), 10, 2 );
}
/**
* This function will safely define a constant
*
* @param string $name The name.
* @param mixed $value The value.
*/
public function define( $name, $value = true ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
/**
* Returns true if has setting.
*
* @param string $name The name.
*/
public function has_setting( $name ) {
return isset( $this->settings[ $name ] );
}
/**
* Returns a setting.
*
* @param string $name The name.
*/
public function get_setting( $name ) {
return isset( $this->settings[ $name ] ) ? $this->settings[ $name ] : null;
}
/**
* Updates a setting.
*
* @param string $name The name.
* @param mixed $value The value.
*/
public function update_setting( $name, $value ) {
$this->settings[ $name ] = $value;
return true;
}
/**
* Returns data.
*
* @param string $name The name.
*/
public function get_data( $name ) {
return isset( $this->data[ $name ] ) ? $this->data[ $name ] : null;
}
/**
* Sets data.
*
* @param string $name The name.
* @param mixed $value The value.
*/
public function set_data( $name, $value ) {
$this->data[ $name ] = $value;
}
/**
* Hook activation
*/
public function activation() {
if ( get_option( 'powerkit_db_version' ) ) {
return;
}
update_option( 'powerkit_db_version', powerkit_raw_setting( 'version' ), true );
}
/**
* Check current version
*/
public function check_version() {
// Version Data.
$new = powerkit_raw_setting( 'version' );
// Get db version.
$current = get_option( 'powerkit_db_version', $new );
// If versions don't match.
if ( ! empty( $current ) && ! empty( $new ) && $current !== $new ) {
/**
* If different versions call a special hook.
*
* @param string $current Current version.
* @param string $new New version.
*/
do_action( 'powerkit_plugin_upgrade', $current, $new );
update_option( 'powerkit_db_version', $new );
} elseif ( ! empty( $new ) && $current !== $new ) {
update_option( 'powerkit_db_version', $new );
}
}
/**
* AMP stylesheets.
*
* @since 1.0.0
*/
public function amp_enqueue_styles() {
powerkit_amp_style( POWERKIT_PATH . 'assets/css/amp.css' );
}
/**
* This function will register scripts and styles for admin dashboard.
*
* @param string $page Current page.
*/
public function admin_enqueue_scripts( $page ) {
wp_enqueue_style( 'powerkit', POWERKIT_URL . 'assets/css/powerkit.css', array(), powerkit_get_setting( 'version' ) );
}
/**
* This function will register scripts and styles
*/
public function wp_enqueue_scripts() {
// Scripts.
if ( is_user_logged_in() ) {
wp_enqueue_script( 'tippy', POWERKIT_URL . 'assets/js/tippy.all.min.js', array( 'jquery' ), powerkit_get_setting( 'version' ), true );
}
wp_enqueue_script( 'powerkit', POWERKIT_URL . 'assets/js/_scripts.js', array( 'jquery', 'tippy' ), powerkit_get_setting( 'version' ), true );
// Icons.
wp_enqueue_style( 'powerkit-icons', POWERKIT_URL . 'assets/fonts/powerkit-icons.woff', array(), powerkit_get_setting( 'version' ) );
wp_style_add_data( 'powerkit-icons', 'alt', 'alternate' );
// Styles.
wp_enqueue_style( 'powerkit', powerkit_style( POWERKIT_URL . 'assets/css/powerkit.css' ), array(), powerkit_get_setting( 'version' ) );
wp_style_add_data( 'powerkit', 'rtl', 'replace' );
}
/**
* Filters the HTML link tag of an enqueued style.
*
* @param string $tag The link tag for the enqueued style.
* @param string $handle The style's registered handle.
*/
public function style_loader_tag( $tag, $handle ) {
if ( 'powerkit-icons' === $handle ) {
return str_replace( "media='all'", "as='font' type='font/wof' crossorigin", $tag );
}
return $tag;
}
/**
* Sets up theme defaults and registers support for various WordPress features.
*/
public function after_setup_theme() {
// Register custom thumbnail sizes.
add_image_size( 'pk-small', 80, 80, true );
add_image_size( 'pk-thumbnail', 300, 225, true );
// Add editor style.
if ( is_rtl() ) {
add_editor_style( powerkit_style( POWERKIT_URL . 'assets/css/powerkit-rtl.css' ) );
} else {
add_editor_style( powerkit_style( POWERKIT_URL . 'assets/css/powerkit.css' ) );
}
}
/**
* Filters the HTML that is allowed for a given context.
*
* @param array $tags Tags by.
* @param string $context Context name.
*/
public function wp_kses_allowed_html( $tags, $context ) {
if ( 'pk-title' === $context ) {
$tags = array(
'a' => array(
'class' => true,
'href' => true,
'title' => true,
'target' => true,
'rel' => true,
),
'div' => array(
'class' => true,
'id' => true,
'style' => true,
),
'span' => array(
'class' => true,
'id' => true,
'style' => true,
),
'img' => array(
'class' => true,
'id' => true,
'src' => true,
'rel' => true,
'srcset' => true,
'size' => true,
),
'br' => array(),
'b' => array(),
'strong' => array(),
'i' => array(),
'p' => array(
'class' => true,
'id' => true,
'style' => true,
),
'h1' => array(
'class' => true,
'id' => true,
'style' => true,
),
'h2' => array(
'class' => true,
'id' => true,
'style' => true,
),
'h3' => array(
'class' => true,
'id' => true,
'style' => true,
),
'h4' => array(
'class' => true,
'id' => true,
'style' => true,
),
'h5' => array(
'class' => true,
'id' => true,
'style' => true,
),
'h6' => array(
'class' => true,
'id' => true,
'style' => true,
),
);
}
return $tags;
}
}
/**
* The main function responsible for returning the one true powerkit Instance to functions everywhere.
* Use this function like you would a global variable, except without needing to declare the global.
*
* Example: <?php $powerkit = powerkit(); ?>
*/
function powerkit() {
// Globals.
global $powerkit_instance;
// Init.
if ( ! isset( $powerkit_instance ) ) {
$powerkit_instance = new Powerkit();
$powerkit_instance->init();
}
return $powerkit_instance;
}
// Initialize.
powerkit();
}
@@ -0,0 +1,118 @@
<?php
/**
* The api functions for the plugin
*
* @package Powerkit
* @subpackage Core
* @version 1.0.0
* @since 1.0.0
*/
/**
* This function will return true for a non empty array
*
* @param array $array Array.
*/
function powerkit_is_array( $array ) {
return ( is_array( $array ) && ! empty( $array ) );
}
/**
* This function will return true for an empty var (allows 0 as true)
*
* @param mixed $value Value.
*/
function powerkit_is_empty( $value ) {
return ( empty( $value ) && ! is_numeric( $value ) );
}
/**
* Alias of powerkit()->has_setting()
*
* @param string $name The name.
*/
function powerkit_has_setting( $name = '' ) {
return powerkit()->has_setting( $name );
}
/**
* Alias of powerkit()->get_setting()
*
* @param string $name The name.
*/
function powerkit_raw_setting( $name = '' ) {
return powerkit()->get_setting( $name );
}
/**
* Alias of powerkit()->update_setting()
*
* @param string $name The name.
* @param mixed $value The value.
*/
function powerkit_update_setting( $name, $value ) {
return powerkit()->update_setting( $name, $value );
}
/**
* Alias of powerkit()->get_setting()
*
* @param string $name The name.
* @param mixed $value The value.
*/
function powerkit_get_setting( $name, $value = null ) {
// Check settings.
if ( powerkit_has_setting( $name ) ) {
$value = powerkit_raw_setting( $name );
}
// Filter.
$value = apply_filters( "powerkit_settings_{$name}", $value );
return $value;
}
/**
* This function will add a value into the settings array found in the acf object
*
* @param string $name The name.
* @param mixed $value The value.
*/
function powerkit_append_setting( $name, $value ) {
// Vars.
$setting = powerkit_raw_setting( $name );
// Bail ealry if not array.
if ( ! is_array( $setting ) ) {
$setting = array();
}
// Append.
$setting[] = $value;
// Update.
return powerkit_update_setting( $name, $setting );
}
/**
* Returns data.
*
* @param string $name The name.
*/
function powerkit_get_data( $name ) {
return powerkit()->get_data( $name );
}
/**
* Sets data.
*
* @param string $name The name.
* @param mixed $value The value.
*/
function powerkit_set_data( $name, $value ) {
return powerkit()->set_data( $name, $value );
}
@@ -0,0 +1,229 @@
<?php
/**
* The basic functions for the plugin and modules
*
* @package Powerkit
* @subpackage Core
* @version 1.0.0
* @since 1.0.0
*/
/**
* This function include files to the plugin.
*
* @param string $dir Directory where you need to search for files.
*/
function powerkit_load_files( $dir ) {
$path = POWERKIT_PATH . $dir;
// Loop through files.
foreach ( glob( $path . '/*' ) as $file ) {
$basename = basename( $file );
if ( is_file( $file ) ) {
require_once wp_normalize_path( $file );
} elseif ( file_exists( sprintf( '%1$s/%2$s/%2$s.php', $path, $basename ) ) ) {
require_once wp_normalize_path( sprintf( '%1$s/%2$s/%2$s.php', $path, $basename ) );
} elseif ( file_exists( sprintf( '%1$s/%2$s/class-powerkit-%2$s.php', $path, $basename ) ) ) {
require_once wp_normalize_path( sprintf( '%1$s/%2$s/class-powerkit-%2$s.php', $path, $basename ) );
}
}
}
/**
* This function checks if the module is enabled
*
* @param array $info The module info.
*/
function powerkit_register_module_info( $info ) {
$modules = (array) powerkit_get_data( 'modules' );
// Slug of module.
$slug = $info['slug'];
// Check exists slug.
if ( $slug ) {
// Enabled load extensions.
$module_enabled = get_option( 'powerkit_enabled_' . $slug, $info['enabled'] );
if ( $module_enabled && 'default' === $info['type'] && $info['load_extensions'] ) {
foreach ( $info['load_extensions'] as $extension ) {
$modules[ $extension ]['enabled'] = true;
}
}
// Add new info.
if ( isset( $modules[ $slug ] ) ) {
$modules[ $slug ] = array_merge( (array) $info, (array) $modules[ $slug ] );
} else {
$modules[ $slug ] = (array) $info;
}
// Update info.
powerkit_set_data( 'modules', $modules );
}
}
/**
* This function return modules.
*/
function powerkit_get_modules() {
$modules = powerkit_get_data( 'modules' );
// Sort modules.
if ( is_array( $modules ) && $modules ) {
$modules_keys = array_keys( $modules );
foreach ( $modules as $key => $row ) {
$modules_priority[ $key ] = $row['priority'];
$modules_name[ $key ] = $row['name'];
}
array_multisort( $modules_priority, SORT_ASC, $modules_name, SORT_ASC, $modules, $modules_keys );
$modules = array_combine( $modules_keys, $modules );
}
return $modules;
}
/**
* This function return meta info of module
*
* @param string $slug The slug.
* @param string $field The field.
*/
function powerkit_get_module_meta( $slug, $field = false ) {
$modules = (array) powerkit_get_data( 'modules' );
if ( $field ) {
if ( isset( $modules[ $slug ][ $field ] ) ) {
return $modules[ $slug ][ $field ];
}
}
if ( isset( $modules[ $slug ] ) ) {
return $modules[ $slug ];
}
}
/**
* This function return value from сonnect module.
*
* @param string $name The field name of сonnect.
*/
function powerkit_connect( $name ) {
$default = array(
'instagram_app_url' => 'https://api.codesupply.co/instagram-connect.php',
'instagram_app_id' => 'MjU5MzMzMzEwNDI0NzQxOA==',
'instagram_app_fb_client_id' => 'MzA1NDQ5ODgzNjk4NjQ5',
'instagram_app_fb_url' => 'https://api.codesupply.co/facebook-connect.php?business=true',
'instagram_app_type' => '',
'instagram_app_access_token' => '',
'instagram_app_user_id' => '',
'instagram_app_username' => '',
'facebook_app_id' => 'MzA1NDQ5ODgzNjk4NjQ5',
'facebook_app_url' => 'https://api.codesupply.co/facebook-connect.php',
'facebook_app_access_token' => '',
'facebook_app_accounts' => '',
'facebook_share_access_token' => 'MTc5NjYwNzk4Nzc0Mjk2JTdDZmV3YV9LRU9TMHBlbHNwUE9md19qcWxqcVRr',
'twitter_app_url' => 'https://api.codesupply.co/twitter-connect.php',
'twitter_app_consumer_key' => '',
'twitter_app_consumer_secret' => '',
'twitter_app_oauth_token' => '',
'twitter_app_oauth_token_secret' => '',
'twitter_app_user_id' => '',
'twitter_app_screen_name' => '',
'youtube_key' => 'QUl6YVN5QUxlUkNTMkVoWThnY0xndlRiTlU3a3g4cXdsVDNLdU9N',
'telegram_token' => 'NTM1NTAwMjM4OkFBR3dUVDBOMDhoeHFPamxHYVhDVDFGa01mb2c2blRnQ2ZR',
'soundcloud_client_id' => 'OTcyMjBmYjM0YWQwMzRiNWQ0YjU5Yjk2N2ZkMTcxN2U=',
'dribbble_token' => '',
'vimeo_token' => 'ODhiMDU4NjA4YWViMmU2MjdiYjc4MmY2MzNkNjVjNjQ=',
'behance_client_id' => 'R0QxcmhQcUpvaWdaN0xqcFFEVEltMkZjOGdPemkxajQ=',
'twitch_client_id' => 'dmk0MDZ5OWhhNDV5MmRzcmtzcDZvMTd1bWt5NTR3',
'vk_token' => 'ZWRkNjQ1ZGU3ZDQ1OTQwZjllMTMyYTMyNmIxM2MxNWJjNWYxMWNhMzRkY2MzYTc1MGE2MmQxOTI4YjY5MjExZThmNTU0Nzc5ZWU3OTNmMTk2YTJiNw==',
);
$name = str_replace( 'powerkit_connect_', '', $name );
// Set value by slug.
$value = get_option( 'powerkit_connect_' . $name );
// Set default value.
if ( ! $value && key_exists( $name, $default ) ) {
if ( base64_encode( base64_decode( $default[ $name ], true ) ) === $default[ $name ] ) {
$value = base64_decode( $default[ $name ] );
} else {
$value = $default[ $name ];
}
}
return $value;
}
/**
* This function checks if the module is enabled
*
* @param string $slug The module slug.
*/
function powerkit_module_enabled( $slug ) {
$module = powerkit_get_module_meta( $slug );
// Default status.
$status = $module['enabled'];
// Check database.
if ( 'default' === $module['type'] ) {
$enabled = get_option( 'powerkit_enabled_' . $slug, $module['enabled'] );
$status = '0' === $enabled ? false : $enabled;
}
return apply_filters( 'powerkit_module_enabled', $status, $slug );
}
/**
* This function return unique slug name to refer to this menu by.
*
* @param string $slug The module slug.
*/
function powerkit_get_page_slug( $slug ) {
return sprintf( 'powerkit_%s', $slug );
}
/**
* This function return admin page url.
*
* @param string $slug The module slug.
* @param string $type The type page.
*/
function powerkit_get_page_url( $slug, $type = 'general' ) {
switch ( $type ) {
case 'general':
return admin_url( sprintf( 'options-general.php?page=%s', powerkit_get_page_slug( $slug ) ) );
case 'writing':
return admin_url( sprintf( 'options-writing.php?page=%s', powerkit_get_page_slug( $slug ) ) );
case 'reading':
return admin_url( sprintf( 'options-reading.php?page=%s', powerkit_get_page_slug( $slug ) ) );
case 'discussion':
return admin_url( sprintf( 'options-reading.php?page=%s', powerkit_get_page_slug( $slug ) ) );
case 'media':
return admin_url( sprintf( 'options-media.php?page=%s', powerkit_get_page_slug( $slug ) ) );
case 'permalink':
return admin_url( sprintf( 'options-permalink.php?page=%s', powerkit_get_page_slug( $slug ) ) );
case 'themes':
return admin_url( sprintf( 'themes.php?page=%s', powerkit_get_page_slug( $slug ) ) );
case 'admin':
return admin_url( sprintf( 'admin.php?page=%s', powerkit_get_page_slug( $slug ) ) );
default:
return admin_url( sprintf( '%s?page=%s', $type, powerkit_get_page_slug( $slug ) ) );
}
}
@@ -0,0 +1,865 @@
<?php
/**
* The basic helpers functions
*
* @package Powerkit
* @subpackage Core
* @version 1.0.0
* @since 1.0.0
*/
/**
* Output error message.
*
* @param string $message The error message.
*/
function powerkit_alert_warning( $message ) {
if ( current_user_can( 'editor' ) || current_user_can( 'administrator' ) ) {
?>
<p class="pk-alert pk-alert-warning" role="alert">
<object>
<?php call_user_func( 'printf', '%s', $message ); ?>
</object>
<?php esc_html_e( ' Dont worry, this error is visible to site admins only, and your site visitors wont see it.', 'powerkit' ); ?>
</p>
<?php
}
}
/**
* Processing path of style.
*
* @param string $path URL to the stylesheet.
*/
function powerkit_style( $path ) {
// Check RTL.
if ( is_rtl() ) {
return $path;
}
// Check Dev.
$dev = POWERKIT_PATH . 'assets/css/powerkit-dev.css';
if ( file_exists( $dev ) ) {
return str_replace( '.css', '-dev.css', $path );
}
return $path;
}
/**
* Generate AMP style.
*
* @param string $path Path to the stylesheet.
*/
function powerkit_amp_style( $path ) {
if ( file_exists( $path ) ) {
$output = call_user_func( 'file_get_contents', $path );
call_user_func( 'printf', '%s', $output );
}
}
/**
* Check post views module.
*
* @return string Type.
*/
function powerkit_post_views_enabled() {
// Post Views Counter.
if ( class_exists( 'Post_Views_Counter' ) ) {
return 'post_views';
}
// Powerkit Post Views.
if ( powerkit_module_enabled( 'post_views' ) ) {
return 'pk_post_views';
}
}
/**
* Process shortcode atts.
*
* @param array $atts Attributes in shortcode tag.
*/
function powerkit_shortcode_atts( $atts ) {
if ( is_array( $atts ) ) {
foreach ( $atts as $name => $val ) {
if ( is_string( $val ) && 'true' === $val ) {
$atts[ $name ] = true;
}
if ( is_string( $val ) && 'false' === $val ) {
$atts[ $name ] = false;
}
}
}
return $atts;
}
/**
* Retrieves a post meta field for the given post ID.
*
* @param int $post_id Post ID.
* @param string $key Optional. The meta key to retrieve. By default, returns
* data for all keys. Default empty.
* @param bool $single Optional. If true, returns only the first value for the specified meta key.
* This parameter has no effect if $key is not specified. Default false.
* @param mixed $default Default value.
* @return mixed Will be an array if $single is false. Will be value of the meta
* field if $single is true.
*/
function powerkit_get_post_metadata( $post_id, $key = '', $single = false, $default = null ) {
if ( ! metadata_exists( 'post', $post_id, $key ) && $default ) {
return $default;
}
return get_metadata( 'post', $post_id, $key, $single );
}
/**
* Get locale in uniform format.
*/
function powerkit_get_locale() {
$locale = get_locale();
if ( preg_match( '#^[a-z]{2}\-[A-Z]{2}$#', $locale ) ) {
$locale = str_replace( '-', '_', $locale );
} elseif ( preg_match( '#^[a-z]{2}$#', $locale ) ) {
if ( function_exists( 'mb_strtoupper' ) ) {
$locale .= '_' . mb_strtoupper( $locale, 'UTF-8' );
} else {
$locale .= '_' . mb_strtoupper( $locale );
}
}
if ( empty( $locale ) ) {
$locale = 'en_US';
}
return apply_filters( 'powerkit_locale', $locale );
}
/**
* Get rounded number.
*
* @param int $number Input number.
* @param int $min_value Minimum value to round number.
* @param int $decimal How may decimals shall be in the rounded number.
*/
function powerkit_get_round_number( $number, $min_value = 1000, $decimal = 1 ) {
if ( $number < $min_value ) {
return number_format_i18n( $number );
}
$alphabets = array(
1000000000 => esc_html__( 'B', 'powerkit' ),
1000000 => esc_html__( 'M', 'powerkit' ),
1000 => esc_html__( 'K', 'powerkit' ),
);
foreach ( $alphabets as $key => $value ) {
if ( $number >= $key ) {
return number_format_i18n( round( $number / $key, $decimal ), $decimal ) . $value;
}
}
}
/**
* Convert dates to readable format
*
* @param string $a Time string (timeformat).
* @return string Formatted time.
*/
function powerkit_relative_time( $a ) {
// Get current timestampt.
$b = strtotime( 'now' );
// Get timestamp when tweet created.
$c = strtotime( $a );
// Get difference.
$d = $b - $c;
// Calculate different time values.
$minute = 60;
$hour = $minute * 60;
$day = $hour * 24;
$week = $day * 7;
if ( is_numeric( $d ) && $d > 0 ) :
// If less then 3 seconds.
if ( $d < 3 ) {
return esc_html__( 'right now', 'powerkit' );
}
// If less then minute.
if ( $d < $minute ) {
return floor( $d ) . esc_html__( ' seconds ago', 'powerkit' );
}
// If less then 2 minutes.
if ( $d < $minute * 2 ) {
return esc_html__( 'about 1 minute ago', 'powerkit' );
}
// If less then hour.
if ( $d < $hour ) {
return floor( $d / $minute ) . esc_html__( ' minutes ago', 'powerkit' );
}
// If less then 2 hours.
if ( $d < $hour * 2 ) {
return esc_html__( 'about 1 hour ago', 'powerkit' );
}
// If less then day.
if ( $d < $day ) {
return floor( $d / $hour ) . esc_html__( ' hours ago', 'powerkit' );
}
// If more then day, but less then 2 days.
if ( $d > $day && $d < $day * 2 ) {
return esc_html__( 'yesterday', 'powerkit' );
}
// If less then year.
if ( $d < $day * 365 ) {
return floor( $d / $day ) . esc_html__( ' days ago', 'powerkit' );
}
// else return more than a year.
return esc_html__( 'over a year ago', 'powerkit' );
endif;
}
/**
* Truncates string with specified length
*
* @param string $string Text string.
* @param int $length Letters length.
* @param string $etc End truncate.
* @param bool $break_words Break words or not.
* @return string
*/
function powerkit_str_truncate( $string, $length = 80, $etc = '&hellip;', $break_words = false ) {
if ( 0 === $length ) {
return '';
}
if ( function_exists( 'mb_strlen' ) ) {
// MultiBite string functions.
if ( mb_strlen( $string ) > $length ) {
$length -= min( $length, mb_strlen( $etc ) );
if ( ! $break_words ) {
$string = preg_replace( '/\s+?(\S+)?$/', '', mb_substr( $string, 0, $length + 1 ) );
}
return mb_substr( $string, 0, $length ) . $etc;
}
} else {
// Default string functions.
if ( strlen( $string ) > $length ) {
$length -= min( $length, strlen( $etc ) );
if ( ! $break_words ) {
$string = preg_replace( '/\s+?(\S+)?$/', '', substr( $string, 0, $length + 1 ) );
}
return substr( $string, 0, $length ) . $etc;
}
}
return $string;
}
/**
* Set number to Short Form
*
* @param int $n The number.
* @param int $decimal The decimal.
*/
function powerkit_abridged_number( $n, $decimal = 1 ) {
// First strip any formatting.
$n = (float) str_replace( ',', '', $n );
// Is this a number?
if ( ! is_numeric( $n ) ) {
return false;
}
// Return current count.
if ( $n < 1000 ) {
return number_format_i18n( $n );
}
// Add suffix.
$suffix = array(
1000000000 => esc_html__( 'B', 'powerkit' ), // Billion.
1000000 => esc_html__( 'M', 'powerkit' ), // Million.
1000 => esc_html__( 'K', 'powerkit' ), // Thousand.
);
foreach ( $suffix as $k => $v ) {
if ( $n >= $k ) {
return number_format_i18n( $n / $k, $decimal ) . $v;
}
}
}
/**
* Time ago
*
* @param string $time The time.
* @return string
*/
function powerkit_timing_ago( $time ) {
$periods = array( esc_html__( 'second', 'powerkit' ), esc_html__( 'minute', 'powerkit' ), esc_html__( 'hour', 'powerkit' ), esc_html__( 'day', 'powerkit' ), esc_html__( 'week', 'powerkit' ), esc_html__( 'month', 'powerkit' ), esc_html__( 'year', 'powerkit' ), esc_html__( 'decade', 'powerkit' ) );
$lengths = array( '60', '60', '24', '7', '4.35', '12', '10' );
$now = time();
$difference = $now - $time;
$tense = esc_html__( 'ago', 'powerkit' );
$lengths_count = count( $lengths );
for ( $j = 0; $difference >= $lengths[ $j ] && $j < $lengths_count - 1; $j++ ) {
$difference /= $lengths[ $j ];
}
$difference = round( $difference );
if ( 1 !== $difference ) {
$periods[ $j ] .= 's';
}
return "$difference $periods[$j] {$tense} ";
}
/**
* Encode data
*
* @param mixed $content The content.
* @param string $secret_key The key.
* @return string
*/
function powerkit_encode_data( $content, $secret_key = 'powerkit' ) {
$content = wp_json_encode( $content );
return base64_encode( $content );
}
/**
* Decode data
*
* @param string $content The content.
* @param string $secret_key The key.
* @return string
*/
function powerkit_decode_data( $content, $secret_key = 'powerkit' ) {
$content = base64_decode( $content );
return json_decode( $content );
}
/**
* Encrypt data
*
* @param mixed $content The content.
* @param string $secret_key The key.
* @return string
*/
function powerkit_encrypt_data( $content, $secret_key = 'powerkit' ) {
$content = maybe_serialize( $content );
if ( function_exists( 'openssl_encrypt' ) && function_exists( 'hash' ) ) {
$encrypt_method = 'AES-256-CBC';
$key = hash( 'sha256', $secret_key );
$iv = substr( hash( 'sha256', 'secret key' ), 0, 16 );
return base64_encode( openssl_encrypt( $content, $encrypt_method, $key, 0, $iv ) );
} else {
return base64_encode( $content );
}
}
/**
* Decrypt data
*
* @param string $content The content.
* @param string $secret_key The key.
* @return string
*/
function powerkit_decrypt_data( $content, $secret_key = 'powerkit' ) {
if ( function_exists( 'openssl_encrypt' ) && function_exists( 'hash' ) ) {
$encrypt_method = 'AES-256-CBC';
$key = hash( 'sha256', $secret_key );
$iv = substr( hash( 'sha256', 'secret key' ), 0, 16 );
$content = openssl_decrypt( base64_decode( $content ), $encrypt_method, $key, 0, $iv );
} else {
$content = base64_decode( $content );
}
$content = maybe_unserialize( $content );
return $content;
}
/**
* Generate uuid hash
*
* @param string $name The name.
* @param string $action The action.
*/
function powerkit_uuid_hash( $name = '_wpnonce', $action = -1 ) {
$user = wp_get_current_user();
$uid = (int) $user->ID;
if ( ! $uid ) {
$uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
}
$token = wp_get_session_token();
$i = wp_nonce_tick();
$hash = substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
if ( ! isset( ${'_REQUEST'}[ $name ] ) ) {
${'_REQUEST'}[ $name ] = $hash;
}
}
/**
* Get the user uuid
*
* @return string
*/
function powerkit_get_user_uuid() {
if ( getenv( 'HTTP_CLIENT_IP' ) ) {
return getenv( 'HTTP_CLIENT_IP' );
} elseif ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) {
return getenv( 'HTTP_X_FORWARDED_FOR' );
} elseif ( getenv( 'HTTP_X_FORWARDED' ) ) {
return getenv( 'HTTP_X_FORWARDED' );
} elseif ( getenv( 'HTTP_FORWARDED_FOR' ) ) {
return getenv( 'HTTP_FORWARDED_FOR' );
} elseif ( getenv( 'HTTP_FORWARDED' ) ) {
return getenv( 'HTTP_FORWARDED' );
} elseif ( getenv( 'REMOTE_ADDR' ) ) {
return getenv( 'REMOTE_ADDR' );
}
return uniqid( 'x', true );
}
/**
* Convert by title Cyrillic characters to Latin characters
*
* @param string $text The text.
*/
function powerkit_text_with_translit( $text ) {
$gost = array(
'А' => 'A',
'Б' => 'B',
'В' => 'V',
'Г' => 'G',
'Ѓ' => 'G`',
'Ґ' => 'G`',
'Д' => 'D',
'Е' => 'E',
'Ё' => 'YO',
'Є' => 'YE',
'Ж' => 'ZH',
'З' => 'Z',
'Ѕ' => 'Z',
'И' => 'I',
'Й' => 'Y',
'Ј' => 'J',
'І' => 'I',
'Ї' => 'YI',
'К' => 'K',
'Ќ' => 'K',
'Л' => 'L',
'Љ' => 'L',
'М' => 'M',
'Н' => 'N',
'Њ' => 'N',
'О' => 'O',
'П' => 'P',
'Р' => 'R',
'С' => 'S',
'Т' => 'T',
'У' => 'U',
'Ў' => 'U',
'Ф' => 'F',
'Х' => 'H',
'Ц' => 'TS',
'Ч' => 'CH',
'Џ' => 'DH',
'Ш' => 'SH',
'Щ' => 'SHH',
'Ъ' => '``',
'Ы' => 'YI',
'Ь' => '`',
'Э' => 'E`',
'Ю' => 'YU',
'Я' => 'YA',
'а' => 'a',
'б' => 'b',
'в' => 'v',
'г' => 'g',
'ѓ' => 'g',
'ґ' => 'g',
'д' => 'd',
'е' => 'e',
'ё' => 'yo',
'є' => 'ye',
'ж' => 'zh',
'з' => 'z',
'ѕ' => 'z',
'и' => 'i',
'й' => 'y',
'ј' => 'j',
'і' => 'i',
'ї' => 'yi',
'к' => 'k',
'ќ' => 'k',
'л' => 'l',
'љ' => 'l',
'м' => 'm',
'н' => 'n',
'њ' => 'n',
'о' => 'o',
'п' => 'p',
'р' => 'r',
'с' => 's',
'т' => 't',
'у' => 'u',
'ў' => 'u',
'ф' => 'f',
'х' => 'h',
'ц' => 'ts',
'ч' => 'ch',
'џ' => 'dh',
'ш' => 'sh',
'щ' => 'shh',
'ь' => '',
'ы' => 'yi',
'ъ' => "'",
'э' => 'e`',
'ю' => 'yu',
'я' => 'ya',
);
return strtr( $text, $gost );
}
/**
* Check social links exists.
*/
function powerkit_social_links_exists() {
if ( ! powerkit_module_enabled( 'social_links' ) ) {
return;
}
if ( get_option( 'powerkit_social_links_multiple_list' ) ) {
return true;
}
}
/**
* Check mailchimp form exists.
*
* @param string $id The list ID.
*/
function powerkit_mailchimp_form_exists( $id = 'default' ) {
if ( ! powerkit_module_enabled( 'opt_in_forms' ) ) {
return;
}
$token = get_option( 'powerkit_mailchimp_token' );
if ( $token ) {
if ( ! $id || 'default' === $id ) {
$id = get_option( 'powerkit_mailchimp_list' );
}
if ( $id ) {
return true;
}
}
}
/**
* Check share buttons exists.
*
* @param string $location The location.
*/
function powerkit_share_buttons_exists( $location ) {
if ( ! powerkit_module_enabled( 'share_buttons' ) ) {
return;
}
if ( ! get_option( "powerkit_share_buttons_{$location}_display" ) ) {
return;
}
$accounts = get_option( "powerkit_share_buttons_{$location}_multiple_list", array( 'facebook', 'twitter', 'pinterest' ) );
if ( $accounts ) {
return true;
}
}
/**
* Get the available image sizes
*/
function powerkit_get_available_image_sizes() {
$wais = & $GLOBALS['_wp_additional_image_sizes'];
$sizes = array();
$image_sizes = get_intermediate_image_sizes();
if ( is_array( $image_sizes ) && $image_sizes ) {
foreach ( $image_sizes as $size ) {
if ( in_array( $size, array( 'thumbnail', 'medium', 'medium_large', 'large' ), true ) ) {
$sizes[ $size ] = array(
'width' => get_option( "{$size}_size_w" ),
'height' => get_option( "{$size}_size_h" ),
'crop' => (bool) get_option( "{$size}_crop" ),
);
} elseif ( isset( $wais[ $size ] ) ) {
$sizes[ $size ] = array(
'width' => $wais[ $size ]['width'],
'height' => $wais[ $size ]['height'],
'crop' => $wais[ $size ]['crop'],
);
}
// Size registered, but has 0 width and height.
if ( 0 === (int) $sizes[ $size ]['width'] && 0 === (int) $sizes[ $size ]['height'] ) {
unset( $sizes[ $size ] );
}
}
}
return $sizes;
}
/**
* Gets the data of a specific image size.
*
* @param string $size Name of the size.
*/
function powerkit_get_image_size( $size ) {
if ( ! is_string( $size ) ) {
return;
}
$sizes = powerkit_get_available_image_sizes();
return isset( $sizes[ $size ] ) ? $sizes[ $size ] : false;
}
/**
* Get the list available image sizes
*/
function powerkit_get_list_available_image_sizes() {
$intermediate_image_sizes = get_intermediate_image_sizes();
$image_sizes = array();
foreach ( $intermediate_image_sizes as $size ) {
$image_sizes[ $size ] = $size;
$data = powerkit_get_image_size( $size );
if ( isset( $data['width'] ) || isset( $data['height'] ) ) {
$width = '~';
$height = '~';
if ( isset( $data['width'] ) && $data['width'] ) {
$width = $data['width'] . 'px';
}
if ( isset( $data['height'] ) && $data['height'] ) {
$height = $data['height'] . 'px';
}
$image_sizes[ $size ] .= sprintf( ' [%s, %s]', $width, $height );
}
}
$image_sizes = apply_filters( 'powerkit_list_available_image_sizes', $image_sizes );
return $image_sizes;
}
/**
* Get fields array for Button in some PK blocks
*
* @param string $field_prefix Field prefix.
* @param string $section_name Section name.
* @param array $active_callback Active callback.
*/
function powerkit_get_gutenberg_button_fields( $field_prefix = 'button', $section_name = '', $active_callback = array() ) {
$fields = array(
array(
'key' => $field_prefix . 'Style',
'label' => esc_html__( 'Style', 'powerkit' ),
'section' => $section_name,
'type' => 'select',
'default' => '',
'choices' => array(
'' => esc_html__( 'Default', 'powerkit' ),
'outline' => esc_html__( 'Outline', 'powerkit' ),
'squared' => esc_html__( 'Squared', 'powerkit' ),
),
'active_callback' => $active_callback,
),
array(
'key' => $field_prefix . 'Size',
'label' => esc_html__( 'Size', 'powerkit' ),
'section' => $section_name,
'type' => 'select',
'default' => '',
'choices' => array(
'' => esc_html__( 'Default', 'powerkit' ),
'sm' => esc_html__( 'Small', 'powerkit' ),
'lg' => esc_html__( 'Large', 'powerkit' ),
),
'active_callback' => $active_callback,
),
array(
'key' => $field_prefix . 'FullWidth',
'label' => esc_html__( 'Full Width', 'powerkit' ),
'section' => $section_name,
'type' => 'toggle',
'default' => false,
'active_callback' => $active_callback,
),
array(
'key' => $field_prefix . 'ColorBg',
'label' => esc_html__( 'Background Color', 'powerkit' ),
'section' => $section_name,
'type' => 'color',
'default' => '',
'output' => array(
array(
'element' => '$ .wp-block-button a.wp-block-button__link',
'property' => 'background-color',
'suffix' => '!important',
),
),
'active_callback' => $active_callback,
),
array(
'key' => $field_prefix . 'ColorBgHover',
'label' => esc_html__( 'Background Color Hover', 'powerkit' ),
'section' => $section_name,
'type' => 'color',
'default' => '',
'output' => array(
array(
'element' => '$ .wp-block-button a.wp-block-button__link:hover, $ .wp-block-button a.wp-block-button__link:focus',
'property' => 'background-color',
'suffix' => '!important',
),
),
'active_callback' => $active_callback,
),
array(
'key' => $field_prefix . 'ColorText',
'label' => esc_html__( 'Text Color', 'powerkit' ),
'section' => $section_name,
'type' => 'color',
'default' => '',
'output' => array(
array(
'element' => '$ .wp-block-button__link',
'property' => 'color',
'suffix' => '!important',
),
),
'active_callback' => $active_callback,
),
array(
'key' => $field_prefix . 'ColorTextHover',
'label' => esc_html__( 'Text Color Hover', 'powerkit' ),
'section' => $section_name,
'type' => 'color',
'default' => '',
'output' => array(
array(
'element' => '$ .wp-block-button a.wp-block-button__link:hover, $ .wp-block-button a.wp-block-button__link:focus',
'property' => 'color',
'suffix' => '!important',
),
),
'active_callback' => $active_callback,
),
);
return $fields;
}
/**
* Print core/button in some PK blocks
*
* @param string $text Text of button.
* @param string $url Url of button.
* @param string $target Target.
* @param string $field_prefix Field prefix.
* @param array $attributes Attributes.
*/
function powerkit_print_gutenberg_blocks_button( $text, $url, $target = '', $field_prefix = 'button', $attributes = array() ) {
$class_name = 'wp-block-button';
$link_class_name = 'wp-block-button__link';
// Style.
if ( isset( $attributes[ $field_prefix . 'Style' ] ) && $attributes[ $field_prefix . 'Style' ] ) {
$class_name .= ' is-style-' . $attributes[ $field_prefix . 'Style' ];
}
// Size.
if ( isset( $attributes[ $field_prefix . 'Size' ] ) && $attributes[ $field_prefix . 'Size' ] ) {
$class_name .= ' is-pk-button-size-' . $attributes[ $field_prefix . 'Size' ];
}
// FullWidth.
if ( isset( $attributes[ $field_prefix . 'FullWidth' ] ) && $attributes[ $field_prefix . 'FullWidth' ] ) {
$class_name .= ' is-pk-button-full-width';
}
// Color.
if ( isset( $attributes[ $field_prefix . 'ColorText' ] ) && $attributes[ $field_prefix . 'ColorText' ] ) {
$link_class_name .= ' has-text-color';
}
// Background.
if ( isset( $attributes[ $field_prefix . 'ColorBg' ] ) && $attributes[ $field_prefix . 'ColorBg' ] ) {
$link_class_name .= ' has-background';
}
?>
<div class="<?php echo esc_attr( $class_name ); ?>">
<a class="<?php echo esc_attr( $link_class_name ); ?>" href="<?php echo esc_url( $url ); ?>" target="<?php echo esc_attr( $target ); ?>">
<?php echo wp_kses_post( $text ); ?>
</a>
</div>
<?php
}
@@ -0,0 +1,354 @@
<?php
/**
* Post Meta Helper Functions
*
* These helper functions return post meta.
*
* @package Powerkit
* @subpackage Core
* @version 1.0.0
* @since 1.0.0
*/
if ( ! function_exists( 'powerkit_allowed_post_meta' ) ) {
/**
* Allowed Post Meta
*
* @param bool $compact If compact version shall be displayed.
* @param array $exclude Exclude list.
*/
function powerkit_allowed_post_meta( $compact = false, $exclude = array() ) {
$allowed = array(
'category' => esc_html__( 'Category', 'powerkit' ),
'date' => esc_html__( 'Date', 'powerkit' ),
'author' => esc_html__( 'Author', 'powerkit' ),
'comments' => esc_html__( 'Comments count', 'powerkit' ),
);
if ( powerkit_post_views_enabled() ) {
$allowed['views'] = esc_html__( 'Views', 'powerkit' );
}
if ( powerkit_module_enabled( 'reading_time' ) ) {
$allowed['reading_time'] = esc_html__( 'Reading time', 'powerkit' );
}
$allowed = apply_filters( 'powerkit_allowed_post_meta', $allowed );
// Computes the difference of arrays.
$allowed = array_diff_key( $allowed, array_flip( (array) $exclude ) );
// If compact.
if ( $compact ) {
return array_keys( $allowed );
}
return $allowed;
}
}
if ( ! function_exists( 'powerkit_block_post_meta' ) ) {
/**
* Block Post Meta
*
* A wrapper function that returns all post meta types either
* in an ordered list <ul> or as a single element <span>.
*
* @param array $settings Settings of block.
* @param mixed $meta Contains post meta types.
* @param bool $echo Echo or return.
* @param bool $compact If meta compact.
*/
function powerkit_block_post_meta( $settings, $meta, $echo = true, $compact = false ) {
$allowed = array();
if ( isset( $settings['showMetaCategory'] ) && $settings['showMetaCategory'] ) {
$allowed[] = 'category';
}
if ( isset( $settings['showMetaAuthor'] ) && $settings['showMetaAuthor'] ) {
$allowed[] = 'author';
}
if ( isset( $settings['showMetaDate'] ) && $settings['showMetaDate'] ) {
$allowed[] = 'date';
}
if ( isset( $settings['showMetaComments'] ) && $settings['showMetaComments'] ) {
$allowed[] = 'comments';
}
if ( isset( $settings['showMetaViews'] ) && $settings['showMetaViews'] ) {
$allowed[] = 'views';
}
if ( isset( $settings['showMetaReadingTime'] ) && $settings['showMetaReadingTime'] ) {
$allowed[] = 'reading_time';
}
if ( isset( $settings['showMetaShares'] ) && $settings['showMetaShares'] ) {
$allowed[] = 'shares';
}
if ( isset( $settings['metaCompact'] ) && $settings['metaCompact'] ) {
$compact = true;
}
if ( ! $allowed ) {
return;
}
powerkit_get_post_meta( $meta, $compact, $echo, $allowed );
}
}
if ( ! function_exists( 'powerkit_get_post_meta' ) ) {
/**
* Post Meta
*
* A wrapper function that returns all post meta types either
* in an ordered list <ul> or as a single element <span>.
*
* @param mixed $meta Contains post meta types.
* @param bool $compact If compact version shall be displayed.
* @param bool $echo Echo or return.
* @param array $allowed Allowed meta types.
*/
function powerkit_get_post_meta( $meta, $compact = false, $echo = true, $allowed = array() ) {
$func_handler = apply_filters( 'powerkit_get_post_meta_handler', false );
if ( $func_handler ) {
return call_user_func( $func_handler, $meta, $compact, $echo, $allowed );
}
// Return if no post meta types provided.
if ( ! $meta ) {
return;
}
// Set default allowed post meta types.
if ( ! $allowed ) {
$allowed = powerkit_allowed_post_meta();
}
if ( is_array( $meta ) ) {
// Intersect provided and allowed meta types.
$meta = array_intersect( $allowed, $meta );
}
$output = null;
if ( $meta && is_array( $meta ) ) {
$output .= '<ul class="pk-post-meta post-meta">';
// Add normal meta types to the list.
foreach ( $meta as $type ) {
$function = "powerkit_get_meta_$type";
if ( function_exists( $function ) ) {
$output .= $function( 'li', $compact );
}
}
$output .= '</ul>';
} else {
if ( in_array( $meta, $allowed, true ) ) {
// Output single meta type.
$function = "powerkit_get_meta_$meta";
if ( function_exists( $function ) ) {
$output .= $function( 'div', $compact );
}
}
}
// If echo is enabled.
if ( $echo ) {
return call_user_func( 'printf', '%s', wp_kses( $output, 'post' ) );
}
return $output;
}
}
if ( ! function_exists( 'powerkit_get_meta_date' ) ) {
/**
* Post Date
*
* @param string $tag Element tag, i.e. div or span.
* @param bool $compact If compact version shall be displayed.
*/
function powerkit_get_meta_date( $tag = 'span', $compact = false ) {
$output = '<' . esc_html( $tag ) . ' class="pk-meta-date meta-date">';
if ( false === $compact ) {
$time_string = get_the_date();
} else {
$time_string = get_the_date( 'd.m.y' );
}
$output .= apply_filters( 'powerkit_post_meta_date_output', '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>' );
$output .= '</' . esc_html( $tag ) . '>';
return $output;
}
}
if ( ! function_exists( 'powerkit_get_meta_author' ) ) {
/**
* Post Author
*
* @param string $tag Element tag, i.e. div or span.
* @param bool $compact If compact version shall be displayed.
*/
function powerkit_get_meta_author( $tag = 'span', $compact = false ) {
$authors = array( get_the_author_meta( 'ID' ) );
$output = '<' . esc_html( $tag ) . ' class="pk-meta-author meta-author">';
$output .= sprintf(
'<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
/* translators: %s: author name */
esc_attr( sprintf( __( 'View all posts by %s', 'powerkit' ), get_the_author() ) ),
// Author.
get_the_author()
);
$output .= '</' . esc_html( $tag ) . '>';
return $output;
}
}
if ( ! function_exists( 'powerkit_get_meta_category' ) ) {
/**
* Post Сategory
*
* @param string $tag Element tag, i.e. div or span.
* @param bool $compact If compact version shall be displayed.
* @param int $post_id Post ID.
*/
function powerkit_get_meta_category( $tag = 'span', $compact = false, $post_id = null ) {
if ( ! $post_id ) {
$post_id = get_the_ID();
}
$output = '<' . esc_html( $tag ) . ' class="pk-meta-category meta-category">';
$output .= get_the_category_list( '', '', $post_id );
$output .= '</' . esc_html( $tag ) . '>';
return $output;
}
}
if ( ! function_exists( 'powerkit_get_meta_comments' ) ) {
/**
* Post Comments
*
* @param string $tag Element tag, i.e. div or span.
* @param bool $compact If compact version shall be displayed.
*/
function powerkit_get_meta_comments( $tag = 'span', $compact = false ) {
$output = '';
$output .= '<' . esc_html( $tag ) . ' class="pk-meta-comments meta-comments">';
if ( true === $compact ) {
$output .= '<i class="pk-icon pk-icon-comment"></i>';
ob_start();
comments_popup_link( '0', '1', '%', 'comments-link', '' );
$output .= ob_get_clean();
} else {
ob_start();
comments_popup_link( esc_html__( 'No comments', 'powerkit' ), esc_html__( 'One comment', 'powerkit' ), '% ' . esc_html__( 'comments', 'powerkit' ), 'comments-link', '' );
$output .= ob_get_clean();
}
$output .= '</' . esc_html( $tag ) . '>';
return $output;
}
}
if ( ! function_exists( 'powerkit_get_meta_reading_time' ) ) {
/**
* Post Reading Time
*
* @param string $tag Element tag, i.e. div or span.
* @param bool $compact If compact version shall be displayed.
*/
function powerkit_get_meta_reading_time( $tag = 'span', $compact = false ) {
if ( ! powerkit_module_enabled( 'reading_time' ) ) {
return;
}
$reading_time = powerkit_get_post_reading_time();
$output = '<' . esc_html( $tag ) . ' class="pk-meta-reading-time meta-reading-time">';
if ( true === $compact ) {
$output .= '<i class="pk-icon pk-icon-watch"></i>' . intval( $reading_time ) . ' ' . esc_html( 'min', 'powerkit' );
} else {
/* translators: %s number of minutes */
$output .= esc_html( sprintf( _n( '%s minute read', '%s minute read', $reading_time, 'powerkit' ), $reading_time ) );
}
$output .= '</' . esc_html( $tag ) . '>';
return $output;
}
}
if ( ! function_exists( 'powerkit_get_meta_views' ) ) {
/**
* Post Reading Time
*
* @param string $tag Element tag, i.e. div or span.
* @param bool $compact If compact version shall be displayed.
*/
function powerkit_get_meta_views( $tag = 'span', $compact = false ) {
switch ( powerkit_post_views_enabled() ) {
case 'post_views':
$views = pvc_get_post_views();
break;
case 'pk_post_views':
$views = powerkit_get_post_views( null, false );
break;
default:
return;
}
// Don't display if minimum threshold is not met.
if ( $views < apply_filters( 'powerkit_post_minimum_views', 1 ) ) {
return;
}
$output = '<' . esc_html( $tag ) . ' class="pk-meta-views meta-views">';
$views_rounded = powerkit_get_round_number( $views );
if ( true === $compact ) {
$output .= '<i class="pk-icon pk-icon-eye"></i>' . esc_html( $views_rounded );
} else {
/* translators: %s number of post views */
$output .= esc_html( sprintf( _n( '%s view', '%s views', $views, 'powerkit' ), $views_rounded ) );
}
$output .= '</' . esc_html( $tag ) . '>';
return $output;
}
}