first commit
This commit is contained in:
@@ -0,0 +1,648 @@
|
||||
<?php
|
||||
/**
|
||||
* Integration of the Gutenberg into the plugin.
|
||||
*
|
||||
* @package Canvas
|
||||
*/
|
||||
|
||||
// If this file is called directly, abort.
|
||||
if ( ! defined( 'WPINC' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
require_once CNVS_PATH . 'gutenberg/block_renderer_controller.php';
|
||||
require_once CNVS_PATH . 'gutenberg/components/fields-css-output/index.php';
|
||||
require_once CNVS_PATH . 'gutenberg/utils/is-field-visible/index.php';
|
||||
require_once CNVS_PATH . 'gutenberg/custom-blocks/index.php';
|
||||
|
||||
/**
|
||||
* Integration Gutenberg class.
|
||||
*/
|
||||
class CNVS_Gutenberg {
|
||||
|
||||
/**
|
||||
* __construct
|
||||
*
|
||||
* This function will initialize the initialize
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
// Define the functionality of the gutenberg.
|
||||
$this->initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize
|
||||
*
|
||||
* This function will initialize the gutenberg.
|
||||
*/
|
||||
public function initialize() {
|
||||
if ( version_compare( get_bloginfo( 'version' ), '5.8', '>=' ) ) {
|
||||
add_action( 'block_categories_all', array( $this, 'block_categories' ) );
|
||||
} else {
|
||||
add_action( 'block_categories', array( $this, 'block_categories' ) );
|
||||
}
|
||||
|
||||
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
|
||||
add_filter( 'widget_block_content', array( $this, 'widget_all_content' ), -1 );
|
||||
add_action( 'wp_head', array( $this, 'content_render_blocks_css' ) );
|
||||
add_action( 'wp_footer', array( $this, 'widget_render_blocks_css' ) );
|
||||
add_action( 'wp_ajax_cnvs_render_thumbnail', array( $this, 'cnvs_render_thumbnail' ) );
|
||||
add_action( 'wp_ajax_nopriv_cnvs_render_thumbnail', array( $this, 'cnvs_render_thumbnail' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the default array of block categories.
|
||||
*
|
||||
* @param array $categories Array of block categories.
|
||||
*/
|
||||
public function block_categories( $categories ) {
|
||||
return array_merge(
|
||||
$categories,
|
||||
array(
|
||||
array(
|
||||
'slug' => 'canvas',
|
||||
'title' => esc_html__( 'Canvas', 'canvas' ),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get breakpoints data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_breakpoints_data() {
|
||||
$sizes = apply_filters(
|
||||
'canvas_register_breakpoints',
|
||||
array(
|
||||
'desktop' => 992, // >= 992
|
||||
'tablet' => 991, // <= 991
|
||||
'mobile' => 575, // <= 575
|
||||
)
|
||||
);
|
||||
|
||||
$data = apply_filters(
|
||||
'canvas_register_breakpoints_data',
|
||||
array(
|
||||
'desktop' => array(
|
||||
'label' => esc_html__( 'Desktop', 'canvas' ),
|
||||
'icon' => '<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M21 2H3c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h7v2H8v2h8v-2h-2v-2h7c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm0 14H3V4h18v12z"/></svg>',
|
||||
),
|
||||
'notebook' => array(
|
||||
'label' => esc_html__( 'Notebook', 'canvas' ),
|
||||
'icon' => '<svg enable-background="new 0 0 64 64" height="24" viewBox="0 0 64 64" width="24" xmlns="http://www.w3.org/2000/svg"><path d="m11.028 45.416h41.943c1.658 0 2.996-1.342 2.996-2.996v-29.96c0-2.206-1.787-3.994-3.992-3.994h-39.949c-2.206 0-3.993 1.788-3.993 3.994v29.96c0 1.654 1.342 2.996 2.995 2.996zm2.005-31.95h37.934v26.95h-37.934z"/><path d="m57.854 47.252h-51.707c-.553 0-1 .447-1 1v3.701c0 1.979 2.009 3.58 4.486 3.58h44.734c2.477 0 4.486-1.602 4.486-3.58v-3.701c.001-.553-.447-1-.999-1zm-19.616 5.141h-12.477c-.552 0-1-.447-1-1s.448-1 1-1h12.477c.553 0 1 .447 1 1s-.447 1-1 1z"/></svg>',
|
||||
),
|
||||
'laptop' => array(
|
||||
'label' => esc_html__( 'Laptop', 'canvas' ),
|
||||
'icon' => '<svg enable-background="new 0 0 64 64" height="24" viewBox="0 0 64 64" width="24" xmlns="http://www.w3.org/2000/svg"><path d="m11.028 45.416h41.943c1.658 0 2.996-1.342 2.996-2.996v-29.96c0-2.206-1.787-3.994-3.992-3.994h-39.949c-2.206 0-3.993 1.788-3.993 3.994v29.96c0 1.654 1.342 2.996 2.995 2.996zm2.005-31.95h37.934v26.95h-37.934z"/><path d="m57.854 47.252h-51.707c-.553 0-1 .447-1 1v3.701c0 1.979 2.009 3.58 4.486 3.58h44.734c2.477 0 4.486-1.602 4.486-3.58v-3.701c.001-.553-.447-1-.999-1zm-19.616 5.141h-12.477c-.552 0-1-.447-1-1s.448-1 1-1h12.477c.553 0 1 .447 1 1s-.447 1-1 1z"/></svg>',
|
||||
),
|
||||
'tablet' => array(
|
||||
'label' => esc_html__( 'Tablet', 'canvas' ),
|
||||
'icon' => '<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M18.5 0h-14C3.12 0 2 1.12 2 2.5v19C2 22.88 3.12 24 4.5 24h14c1.38 0 2.5-1.12 2.5-2.5v-19C21 1.12 19.88 0 18.5 0zm-7 23c-.83 0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5 1.5.67 1.5 1.5-.67 1.5-1.5 1.5zm7.5-4H4V3h15v16z"/></svg>',
|
||||
),
|
||||
'mobile' => array(
|
||||
'label' => esc_html__( 'Mobile', 'canvas' ),
|
||||
'icon' => '<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M15.5 1h-8C6.12 1 5 2.12 5 3.5v17C5 21.88 6.12 23 7.5 23h8c1.38 0 2.5-1.12 2.5-2.5v-17C18 2.12 16.88 1 15.5 1zm-4 21c-.83 0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5 1.5.67 1.5 1.5-.67 1.5-1.5 1.5zm4.5-4H7V4h9v14z"/></svg>',
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$result = array();
|
||||
|
||||
foreach ( $sizes as $name => $width ) {
|
||||
$result[ $name ] = array(
|
||||
'label' => isset( $data[ $name ]['label'] ) ? $data[ $name ]['label'] : $name,
|
||||
'icon' => isset( $data[ $name ]['icon'] ) ? $data[ $name ]['icon'] : '',
|
||||
'width' => $width,
|
||||
);
|
||||
}
|
||||
|
||||
// Sort from larger screens to smaller.
|
||||
uasort(
|
||||
$result,
|
||||
function ( $a, $b ) {
|
||||
return $b['width'] - $a['width'];
|
||||
}
|
||||
);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get schemes data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_schemes_data() {
|
||||
$result = array();
|
||||
|
||||
if ( get_theme_support( 'canvas-enable-data-scheme' ) ) {
|
||||
$schemes = array(
|
||||
'default' => array(
|
||||
'label' => esc_html__( 'Default', 'canvas' ),
|
||||
'icon' => '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>',
|
||||
),
|
||||
'dark' => array(
|
||||
'label' => esc_html__( 'Dark', 'canvas' ),
|
||||
'icon' => '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>',
|
||||
),
|
||||
);
|
||||
|
||||
foreach ( $schemes as $key => $data ) {
|
||||
$result[ $key ] = array(
|
||||
'label' => isset( $data['label'] ) ? $data['label'] : $key,
|
||||
'icon' => isset( $data['icon'] ) ? $data['icon'] : '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all breakpoints data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_all_breakpoints_data() {
|
||||
$schemes = cnvs_gutenberg()->get_schemes_data();
|
||||
$breakpoints = cnvs_gutenberg()->get_breakpoints_data();
|
||||
|
||||
// Combine scheme and breakpoints.
|
||||
foreach ( $schemes as $name => $scheme ) {
|
||||
if ( $name && 'default' !== $name ) {
|
||||
foreach ( $breakpoints as $res => $breakpoint ) {
|
||||
if ( $res && 'desktop' !== $res ) {
|
||||
$breakpoints[ $name . '_' . $res ] = $breakpoint;
|
||||
$breakpoints[ $name . '_' . $res ]['scheme'] = $name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $breakpoints;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue block editor specific scripts.
|
||||
*/
|
||||
public function enqueue_block_editor_assets() {
|
||||
wp_enqueue_script(
|
||||
'canvas-gutenberg',
|
||||
CNVS_URL . 'gutenberg/index.js',
|
||||
array( 'wp-editor', 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-edit-post', 'wp-compose', 'wp-components' ),
|
||||
filemtime( CNVS_PATH . 'gutenberg/index.js' )
|
||||
);
|
||||
wp_localize_script( 'jquery-ui-core', 'canvasAllBreakpoints', $this->get_all_breakpoints_data() );
|
||||
wp_localize_script( 'jquery-ui-core', 'canvasBreakpoints', $this->get_breakpoints_data() );
|
||||
wp_localize_script( 'jquery-ui-core', 'canvasSchemes', $this->get_schemes_data() );
|
||||
|
||||
if ( get_theme_support( 'canvas-enable-data-scheme' ) ) {
|
||||
wp_enqueue_script(
|
||||
'canvas-schemes',
|
||||
CNVS_URL . 'gutenberg/schemes/index.js',
|
||||
array( 'wp-editor', 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-edit-post', 'wp-compose', 'wp-components' ),
|
||||
filemtime( CNVS_PATH . 'gutenberg/schemes/index.js' )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get content all widget
|
||||
*
|
||||
* @param string $content The content.
|
||||
*/
|
||||
public function widget_all_content( $content ) {
|
||||
global $canvas_widget_all_content;
|
||||
|
||||
if ( ! $canvas_widget_all_content ) {
|
||||
$canvas_widget_all_content = __return_empty_string();
|
||||
}
|
||||
|
||||
$canvas_widget_all_content .= $content;
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse blocks and prepare styles
|
||||
*
|
||||
* @param array $blocks Blocks array with attributes.
|
||||
* @return string
|
||||
*/
|
||||
public function parse_blocks_css( $blocks ) {
|
||||
$schemes = cnvs_gutenberg()->get_schemes_data();
|
||||
$breakpoints = cnvs_gutenberg()->get_breakpoints_data();
|
||||
|
||||
$styles = '';
|
||||
|
||||
// Reusable blocks.
|
||||
foreach ( $blocks as $key => $block ) {
|
||||
if ( isset( $block['blockName'] ) && isset( $block['attrs']['ref'] ) ) {
|
||||
if ( 'core/block' === $block['blockName'] ) {
|
||||
$gcontent = get_post( $block['attrs']['ref'] );
|
||||
|
||||
if ( $gcontent ) {
|
||||
$gblocks = parse_blocks( $gcontent->post_content );
|
||||
|
||||
$blocks[ $key ]['innerBlocks'] = $gblocks;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Loop blocks.
|
||||
foreach ( $blocks as $block ) {
|
||||
if ( isset( $block['attrs'] ) ) {
|
||||
if ( isset( $block['attrs']['canvasClassName'] ) && $block['attrs']['canvasClassName'] ) {
|
||||
$css_selector = '.' . $block['attrs']['canvasClassName'];
|
||||
|
||||
/**
|
||||
* Background Image.
|
||||
*/
|
||||
$background_image_css = '';
|
||||
|
||||
foreach ( $breakpoints as $name => $breakpoint ) {
|
||||
$suffix = '';
|
||||
$media_background_image_css = '';
|
||||
|
||||
if ( $name && 'desktop' !== $name ) {
|
||||
$suffix = '_' . $name;
|
||||
}
|
||||
|
||||
if ( isset( $block['attrs'][ 'backgroundImage' . $suffix ] ) && $block['attrs'][ 'backgroundImage' . $suffix ]['id'] ) {
|
||||
$media_background_image_css .= 'background-image: url("' . esc_attr( $block['attrs'][ 'backgroundImage' . $suffix ]['url'] ) . '") !important;';
|
||||
}
|
||||
|
||||
if ( isset( $block['attrs']['backgroundImage'] ) && $block['attrs']['backgroundImage']['id'] ) {
|
||||
|
||||
if ( isset( $block['attrs'][ 'backgroundPosition' . $suffix ] ) ) {
|
||||
if ( 'custom' === $block['attrs'][ 'backgroundPosition' . $suffix ] ) {
|
||||
$unit_x = isset( $block['attrs'][ 'backgroundPositionXUnit' . $suffix ] ) ? $block['attrs'][ 'backgroundPositionXUnit' . $suffix ] : 'px';
|
||||
$val_x = isset( $block['attrs'][ 'backgroundPositionXVal' . $suffix ] ) ? $block['attrs'][ 'backgroundPositionXVal' . $suffix ] : '0';
|
||||
$unit_y = isset( $block['attrs'][ 'backgroundPositionYUnit' . $suffix ] ) ? $block['attrs'][ 'backgroundPositionYUnit' . $suffix ] : 'px';
|
||||
$val_y = isset( $block['attrs'][ 'backgroundPositionYVal' . $suffix ] ) ? $block['attrs'][ 'backgroundPositionYVal' . $suffix ] : '0';
|
||||
|
||||
$media_background_image_css .= 'background-position: ' . esc_attr( $val_x ) . esc_attr( $unit_x ) . ' ' . esc_attr( $val_y ) . esc_attr( $unit_y ) . ' !important;';
|
||||
} else {
|
||||
$media_background_image_css .= 'background-position: ' . esc_attr( $block['attrs'][ 'backgroundPosition' . $suffix ] ) . ' !important;';
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $block['attrs'][ 'backgroundAttachment' . $suffix ] ) ) {
|
||||
$media_background_image_css .= 'background-attachment: ' . esc_attr( $block['attrs'][ 'backgroundAttachment' . $suffix ] ) . ' !important;';
|
||||
}
|
||||
|
||||
if ( isset( $block['attrs'][ 'backgroundRepeat' . $suffix ] ) ) {
|
||||
$media_background_image_css .= 'background-repeat: ' . esc_attr( $block['attrs'][ 'backgroundRepeat' . $suffix ] ) . ' !important;';
|
||||
}
|
||||
|
||||
if ( isset( $block['attrs'][ 'backgroundSize' . $suffix ] ) ) {
|
||||
|
||||
if ( 'custom' === $block['attrs'][ 'backgroundSize' . $suffix ] ) {
|
||||
|
||||
if ( isset( $block['attrs'][ 'backgroundSizeVal' . $suffix ] ) ) {
|
||||
$unit = isset( $block['attrs'][ 'backgroundSizeUnit' . $suffix ] ) ? $block['attrs'][ 'backgroundSizeUnit' . $suffix ] : '%';
|
||||
|
||||
$media_background_image_css .= 'background-size: ' . esc_attr( $block['attrs'][ 'backgroundSizeVal' . $suffix ] ) . esc_attr( $unit ) . ' auto !important;';
|
||||
}
|
||||
} else {
|
||||
$media_background_image_css .= 'background-size: ' . esc_attr( $block['attrs'][ 'backgroundSize' . $suffix ] ) . ' !important;';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( $suffix && $media_background_image_css ) {
|
||||
$background_image_css .= '@media (max-width: ' . $breakpoint['width'] . 'px) { ' . $css_selector . ' { ' . $media_background_image_css . ' } } ';
|
||||
} elseif ( $media_background_image_css ) {
|
||||
$background_image_css .= $css_selector . ' { ' . $media_background_image_css . ' } ';
|
||||
}
|
||||
}
|
||||
|
||||
if ( $background_image_css ) {
|
||||
$background_image_css_styles = $background_image_css;
|
||||
// Background image styles filter.
|
||||
$background_image_css = apply_filters( "canvas_blocks_dynamic_css_background_image_{$block['blockName']}", $background_image_css, $css_selector, $background_image_css_styles, $block );
|
||||
}
|
||||
|
||||
/**
|
||||
* Spacings.
|
||||
*/
|
||||
$spacings_css = '';
|
||||
|
||||
// Available spacings.
|
||||
$all_spacings = array(
|
||||
'marginTop' => 'margin-top',
|
||||
'marginBottom' => 'margin-bottom',
|
||||
'marginLeft' => 'margin-left',
|
||||
'marginRight' => 'margin-right',
|
||||
'paddingTop' => 'padding-top',
|
||||
'paddingBottom' => 'padding-bottom',
|
||||
'paddingLeft' => 'padding-left',
|
||||
'paddingRight' => 'padding-right',
|
||||
);
|
||||
|
||||
foreach ( $breakpoints as $name => $breakpoint ) {
|
||||
$suffix = '';
|
||||
$margin_unit = 'px';
|
||||
$padding_unit = 'px';
|
||||
$media_spacings_css = '';
|
||||
|
||||
if ( $name && 'desktop' !== $name ) {
|
||||
$suffix = '_' . $name;
|
||||
}
|
||||
|
||||
if ( isset( $block['attrs'][ 'marginUnit' . $suffix ] ) && $block['attrs'][ 'marginUnit' . $suffix ] ) {
|
||||
$margin_unit = $block['attrs'][ 'marginUnit' . $suffix ];
|
||||
}
|
||||
if ( isset( $block['attrs'][ 'paddingUnit' . $suffix ] ) && $block['attrs'][ 'paddingUnit' . $suffix ] ) {
|
||||
$padding_unit = $block['attrs'][ 'paddingUnit' . $suffix ];
|
||||
}
|
||||
|
||||
foreach ( $all_spacings as $space => $prop ) {
|
||||
if ( isset( $block['attrs'][ $space . $suffix ] ) ) {
|
||||
$current_unit = strpos( $space, 'margin' ) === 0 ? $margin_unit : $padding_unit;
|
||||
$media_spacings_css .= $prop . ': ' . esc_attr( $block['attrs'][ $space . $suffix ] ) . $current_unit . ' !important; ';
|
||||
}
|
||||
}
|
||||
|
||||
if ( $suffix && $media_spacings_css ) {
|
||||
$spacings_css .= '@media (max-width: ' . $breakpoint['width'] . 'px) { ' . $css_selector . ' { ' . $media_spacings_css . ' } } ';
|
||||
} elseif ( $media_spacings_css ) {
|
||||
$spacings_css .= $css_selector . ' { ' . $media_spacings_css . ' } ';
|
||||
}
|
||||
}
|
||||
|
||||
if ( $spacings_css ) {
|
||||
$spacings_css_styles = $spacings_css;
|
||||
|
||||
// Spacings styles filter.
|
||||
$spacings_css = apply_filters( "canvas_blocks_dynamic_css_spacings_{$block['blockName']}", $spacings_css, $css_selector, $spacings_css_styles, $block );
|
||||
}
|
||||
|
||||
/**
|
||||
* Borders Radius.
|
||||
*/
|
||||
$borders_radius_css = '';
|
||||
|
||||
// Available borders radius.
|
||||
$all_borders_radius = array(
|
||||
'borderRadiusTopLeft' => 'border-top-left-radius',
|
||||
'borderRadiusTopRight' => 'border-top-right-radius',
|
||||
'borderRadiusBottomLeft' => 'border-bottom-left-radius',
|
||||
'borderRadiusBottomRight' => 'border-bottom-right-radius',
|
||||
);
|
||||
|
||||
foreach ( $breakpoints as $name => $breakpoint ) {
|
||||
$suffix = '';
|
||||
$border_radius_unit = 'px';
|
||||
$media_border_radius_css = '';
|
||||
|
||||
if ( $name && 'desktop' !== $name ) {
|
||||
$suffix = '_' . $name;
|
||||
}
|
||||
|
||||
if ( isset( $block['attrs'][ 'borderRadiusUnit' . $suffix ] ) && $block['attrs'][ 'borderRadiusUnit' . $suffix ] ) {
|
||||
$border_radius_unit = $block['attrs'][ 'borderRadiusUnit' . $suffix ];
|
||||
}
|
||||
|
||||
foreach ( $all_borders_radius as $space => $prop ) {
|
||||
if ( isset( $block['attrs'][ $space . $suffix ] ) ) {
|
||||
$media_border_radius_css .= $prop . ': ' . esc_attr( $block['attrs'][ $space . $suffix ] ) . $border_radius_unit . ' !important; ';
|
||||
}
|
||||
}
|
||||
|
||||
if ( $suffix && $media_border_radius_css ) {
|
||||
$borders_radius_css .= '@media (max-width: ' . $breakpoint['width'] . 'px) { ' . $css_selector . ' { ' . $media_border_radius_css . ' } } ';
|
||||
} elseif ( $media_border_radius_css ) {
|
||||
$borders_radius_css .= $css_selector . ' { ' . $media_border_radius_css . ' } ';
|
||||
}
|
||||
}
|
||||
|
||||
if ( $borders_radius_css ) {
|
||||
$borders_radius_css_styles = $borders_radius_css;
|
||||
|
||||
// Border styles filter.
|
||||
$borders_radius_css = apply_filters( "canvas_blocks_dynamic_css_border_radius_{$block['blockName']}", $borders_radius_css, $css_selector, $borders_radius_css_styles, $block );
|
||||
}
|
||||
|
||||
/**
|
||||
* Borders.
|
||||
*/
|
||||
$borders_css = '';
|
||||
|
||||
// Available borders.
|
||||
$all_borders = array(
|
||||
'borderWidthTop' => 'border-top-width',
|
||||
'borderWidthBottom' => 'border-bottom-width',
|
||||
'borderWidthLeft' => 'border-left-width',
|
||||
'borderWidthRight' => 'border-right-width',
|
||||
);
|
||||
|
||||
if ( isset( $block['attrs']['borderStyle'] ) && $block['attrs']['borderStyle'] ) {
|
||||
$main_border_css = 'border-style: ' . $block['attrs']['borderStyle'] . ';';
|
||||
$main_border_css .= 'border-width: 0;';
|
||||
|
||||
$borders_css .= $css_selector . ' { ' . $main_border_css . ' } ';
|
||||
|
||||
foreach ( $schemes as $name => $scheme ) {
|
||||
$rule = '';
|
||||
$suffix = '';
|
||||
|
||||
if ( $name && 'default' !== $name ) {
|
||||
$suffix = '_' . $name;
|
||||
|
||||
$rule = sprintf( '[data-scheme="%s"] ', $name );
|
||||
}
|
||||
|
||||
if ( isset( $block['attrs'][ 'borderColor' . $suffix ] ) ) {
|
||||
$main_border_css = 'border-color: ' . $block['attrs'][ 'borderColor' . $suffix ] . ';';
|
||||
|
||||
$borders_css .= $rule . $css_selector . ' { ' . $main_border_css . ' } ';
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $breakpoints as $name => $breakpoint ) {
|
||||
$suffix = '';
|
||||
$border_width_unit = 'px';
|
||||
$media_border_css = '';
|
||||
|
||||
if ( $name && 'desktop' !== $name ) {
|
||||
$suffix = '_' . $name;
|
||||
}
|
||||
|
||||
if ( isset( $block['attrs'][ 'borderWidthUnit' . $suffix ] ) && $block['attrs'][ 'borderWidthUnit' . $suffix ] ) {
|
||||
$border_width_unit = $block['attrs'][ 'borderWidthUnit' . $suffix ];
|
||||
}
|
||||
|
||||
foreach ( $all_borders as $space => $prop ) {
|
||||
if ( isset( $block['attrs'][ $space . $suffix ] ) ) {
|
||||
$media_border_css .= $prop . ': ' . esc_attr( $block['attrs'][ $space . $suffix ] ) . $border_width_unit . ' !important; ';
|
||||
}
|
||||
}
|
||||
|
||||
if ( $suffix && $media_border_css ) {
|
||||
$borders_css .= '@media (max-width: ' . $breakpoint['width'] . 'px) { ' . $css_selector . ' { ' . $media_border_css . ' } } ';
|
||||
} elseif ( $media_border_css ) {
|
||||
$borders_css .= $css_selector . ' { ' . $media_border_css . ' } ';
|
||||
}
|
||||
}
|
||||
|
||||
if ( $borders_css ) {
|
||||
$borders_css_styles = $borders_css;
|
||||
|
||||
// Border styles filter.
|
||||
$borders_css = apply_filters( "canvas_blocks_dynamic_css_border_{$block['blockName']}", $borders_css, $css_selector, $borders_css_styles, $block );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Responsive Display.
|
||||
*/
|
||||
$resp_css = '';
|
||||
|
||||
foreach ( $breakpoints as $name => $breakpoint ) {
|
||||
$resp_media = '';
|
||||
|
||||
switch ( $name ) {
|
||||
case 'mobile':
|
||||
$resp_media = '(max-width: ' . $breakpoints['mobile']['width'] . 'px)';
|
||||
break;
|
||||
case 'tablet':
|
||||
$resp_media = '(min-width: ' . ( $breakpoints['mobile']['width'] + 1 ) . 'px) and (max-width: ' . $breakpoints['tablet']['width'] . 'px)';
|
||||
break;
|
||||
case 'notebook':
|
||||
$resp_media = '(min-width: ' . ( $breakpoints['tablet']['width'] + 1 ) . 'px) and (max-width: ' . $breakpoints['notebook']['width'] . 'px)';
|
||||
break;
|
||||
case 'laptop':
|
||||
$resp_media = '(min-width: ' . ( $breakpoints['tablet']['width'] + 1 ) . 'px) and (max-width: ' . $breakpoints['laptop']['width'] . 'px)';
|
||||
break;
|
||||
case 'desktop':
|
||||
$resp_media = '(min-width: ' . ( $breakpoints['desktop']['width'] ) . 'px)';
|
||||
break;
|
||||
}
|
||||
|
||||
if ( isset( $block['attrs'][ 'canvasResponsiveHide_' . $name ] ) && $block['attrs'][ 'canvasResponsiveHide_' . $name ] ) {
|
||||
$resp_css .= '@media ' . $resp_media . ' { ' . $css_selector . ' { display: none !important; } } ';
|
||||
}
|
||||
|
||||
$resp_prev_breakpoint = $breakpoint;
|
||||
}
|
||||
|
||||
// Concat.
|
||||
$styles .= $background_image_css . $spacings_css . $borders_radius_css . $borders_css . $resp_css;
|
||||
}
|
||||
|
||||
// All styles filter.
|
||||
$styles = apply_filters( "canvas_blocks_dynamic_css_{$block['blockName']}", $styles, $block );
|
||||
}
|
||||
|
||||
// Inner blocks.
|
||||
if ( isset( $block['innerBlocks'] ) && ! empty( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) {
|
||||
$styles .= $this->parse_blocks_css( $block['innerBlocks'] );
|
||||
}
|
||||
}
|
||||
|
||||
// All blocks styles filter.
|
||||
$styles = apply_filters( 'canvas_blocks_dynamic_css', $styles, $blocks );
|
||||
|
||||
return $styles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Content render blocks CSS.
|
||||
*/
|
||||
public function content_render_blocks_css() {
|
||||
if ( ! function_exists( 'has_blocks' ) || ! function_exists( 'parse_blocks' ) || ! has_blocks( get_the_ID() ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
global $post;
|
||||
|
||||
if ( ! is_object( $post ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$blocks = parse_blocks( $post->post_content );
|
||||
|
||||
if ( ! is_array( $blocks ) || empty( $blocks ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$style = "\n<style type=\"text/css\" media=\"all\" id=\"canvas-blocks-dynamic-styles\">\n";
|
||||
$style .= $this->parse_blocks_css( $blocks );
|
||||
$style .= "\n</style>\n";
|
||||
|
||||
echo (string) $style; // XSS.
|
||||
}
|
||||
|
||||
/**
|
||||
* Widget render blocks CSS.
|
||||
*/
|
||||
public function widget_render_blocks_css() {
|
||||
global $canvas_widget_all_content;
|
||||
|
||||
if ( ! function_exists( 'has_blocks' ) || ! function_exists( 'parse_blocks' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
global $canvas_widget_all_content;
|
||||
|
||||
if ( ! $canvas_widget_all_content ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$blocks = parse_blocks( $canvas_widget_all_content );
|
||||
|
||||
if ( ! is_array( $blocks ) || empty( $blocks ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$style = "\n<style type=\"text/css\" media=\"all\" id=\"canvas-widget-blocks-dynamic-styles\">\n";
|
||||
$style .= $this->parse_blocks_css( $blocks );
|
||||
$style .= "\n</style>\n";
|
||||
|
||||
echo (string) $style; // XSS.
|
||||
}
|
||||
|
||||
/**
|
||||
* Render thumbnail.
|
||||
*/
|
||||
public function cnvs_render_thumbnail() {
|
||||
if ( isset( $_GET['image_id'] ) ) { // Input var ok.
|
||||
$image_id = sanitize_text_field( wp_unslash( $_GET['image_id'] ) ); // Input var ok.
|
||||
|
||||
$image_url = wp_get_attachment_image_url( $image_id, 'thumbnail' );
|
||||
|
||||
if ( $image_url ) {
|
||||
header( 'Location: ' . $image_url );
|
||||
}
|
||||
}
|
||||
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The main function responsible for returning the one true canvas Instance to functions everywhere.
|
||||
* Use this function like you would a global variable, except without needing to declare the global.
|
||||
*
|
||||
* Example: <?php $cnvs_gutenberg = cnvs_gutenberg(); ?>
|
||||
*/
|
||||
function cnvs_gutenberg() {
|
||||
|
||||
// Globals.
|
||||
global $cnvs_gutenberg_instance;
|
||||
|
||||
// Init.
|
||||
if ( ! isset( $cnvs_gutenberg_instance ) ) {
|
||||
$cnvs_gutenberg_instance = new CNVS_Gutenberg();
|
||||
}
|
||||
|
||||
return $cnvs_gutenberg_instance;
|
||||
}
|
||||
|
||||
// Initialize.
|
||||
cnvs_gutenberg();
|
||||
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
/**
|
||||
* Integration of the Layouts into the plugin.
|
||||
*
|
||||
* @package Canvas
|
||||
*/
|
||||
|
||||
// If this file is called directly, abort.
|
||||
if ( ! defined( 'WPINC' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
/**
|
||||
* Integration Layouts class.
|
||||
*/
|
||||
class CNVS_Layouts {
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
// Register custom post type.
|
||||
add_action( 'init', array( $this, 'add_custom_post_type' ) );
|
||||
|
||||
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
|
||||
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register custom post type.
|
||||
*/
|
||||
public function add_custom_post_type() {
|
||||
register_post_type(
|
||||
'canvas_layout',
|
||||
array(
|
||||
'label' => __( 'Canvas Layouts', 'canvas' ),
|
||||
'public' => false,
|
||||
'publicly_queryable' => false,
|
||||
'has_archive' => false,
|
||||
'show_ui' => true,
|
||||
'exclude_from_search' => true,
|
||||
'show_in_nav_menus' => false,
|
||||
'rewrite' => false,
|
||||
'menu_icon' => 'dashicons-editor-table',
|
||||
'hierarchical' => false,
|
||||
'show_in_menu' => false,
|
||||
'show_in_admin_bar' => false,
|
||||
'show_in_rest' => true,
|
||||
'capability_type' => 'post',
|
||||
'supports' => array(
|
||||
'title',
|
||||
'editor',
|
||||
'thumbnail',
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get layouts data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_layouts_data() {
|
||||
// Predefined layouts.
|
||||
$layouts = apply_filters( 'canvas_register_layouts', array() );
|
||||
|
||||
$categories = apply_filters( 'canvas_register_layouts_categories', array() );
|
||||
|
||||
if ( $layouts ) {
|
||||
// Parse layouts JSON.
|
||||
foreach ( $layouts as $k => $data ) {
|
||||
$json = file_get_contents( $data['json'] );
|
||||
$json = json_decode( $json, true );
|
||||
|
||||
if ( isset( $json['content'] ) ) {
|
||||
$layouts[ $k ]['content'] = $json['content'];
|
||||
}
|
||||
}
|
||||
|
||||
// Get layouts from DB.
|
||||
global $post;
|
||||
$backup_global_post = $post;
|
||||
|
||||
$local_layouts_query = new WP_Query(
|
||||
array(
|
||||
'post_type' => 'canvas_layout',
|
||||
'posts_per_page' => -1,
|
||||
'showposts' => -1,
|
||||
'paged' => -1,
|
||||
)
|
||||
);
|
||||
|
||||
while ( $local_layouts_query->have_posts() ) {
|
||||
$local_layouts_query->the_post();
|
||||
$db_template = get_post();
|
||||
|
||||
$image_id = get_post_thumbnail_id( $db_template->ID );
|
||||
$image_data = wp_get_attachment_image_src( $image_id, 'large' );
|
||||
|
||||
$layouts[ 'db-layout-' . $db_template->ID ] = array(
|
||||
'title' => $db_template->post_title,
|
||||
'content' => $db_template->post_content,
|
||||
'thumbnail' => isset( $image_data[0] ) ? $image_data[0] : false,
|
||||
'category' => array( 'db-layouts' ),
|
||||
);
|
||||
|
||||
// add category.
|
||||
if ( ! isset( $categories['db-layouts'] ) ) {
|
||||
$categories['db-layouts'] = esc_html__( 'User Layouts', 'canvas' );
|
||||
}
|
||||
}
|
||||
|
||||
wp_reset_postdata();
|
||||
|
||||
$post = $backup_global_post;
|
||||
}
|
||||
|
||||
return array(
|
||||
'layouts' => $layouts,
|
||||
'categories' => $categories,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue block editor specific scripts.
|
||||
*/
|
||||
public function enqueue_block_editor_assets() {
|
||||
wp_enqueue_script(
|
||||
'canvas-layouts',
|
||||
CNVS_URL . 'layouts/extension/index.js',
|
||||
array( 'wp-editor', 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-edit-post', 'wp-compose', 'wp-components' ),
|
||||
filemtime( CNVS_PATH . 'layouts/extension/index.js' )
|
||||
);
|
||||
wp_localize_script( 'jquery-ui-core', 'canvasLayouts', $this->get_layouts_data() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue admin layouts list scripts.
|
||||
*/
|
||||
public function admin_enqueue_scripts() {
|
||||
$screen = get_current_screen();
|
||||
|
||||
if ( isset( $screen->id ) && 'edit-canvas_layout' === $screen->id ) {
|
||||
wp_enqueue_style( 'wp-components' );
|
||||
wp_enqueue_script(
|
||||
'canvas-layouts',
|
||||
CNVS_URL . 'layouts/admin-list/index.js',
|
||||
array( 'wp-editor', 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-edit-post', 'wp-compose', 'wp-components', 'wp-api-fetch', 'lodash' ),
|
||||
filemtime( CNVS_PATH . 'layouts/admin-list/index.js' ),
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new CNVS_Layouts();
|
||||
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
/**
|
||||
* Page Templates
|
||||
*
|
||||
* @package Canvas
|
||||
*/
|
||||
|
||||
// If this file is called directly, abort.
|
||||
if ( ! defined( 'WPINC' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
/**
|
||||
* Module main class.
|
||||
*/
|
||||
class CNVS_Page_Templates {
|
||||
|
||||
/**
|
||||
* The array of templates that this plugin tracks.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $templates;
|
||||
|
||||
/**
|
||||
* Initialize the class and set its properties.
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
$this->templates = array();
|
||||
|
||||
add_filter( 'theme_page_templates', array( $this, 'add_new_template' ) );
|
||||
|
||||
// Add a filter to the page attributes metabox to inject our template into the page template cache.
|
||||
add_filter( 'page_attributes_dropdown_pages_args', array( $this, 'register_project_templates' ) );
|
||||
|
||||
// Add a filter to the save post in order to inject out template into the page cache.
|
||||
add_filter( 'wp_insert_post_data', array( $this, 'register_project_templates' ) );
|
||||
|
||||
// Add a filter to the template include in order to determine if the page has our template assigned and return it's path.
|
||||
add_filter( 'template_include', array( $this, 'view_project_template' ) );
|
||||
|
||||
// Add your templates to this array.
|
||||
$this->templates = array(
|
||||
'template-canvas-fullwidth.php' => esc_html__( 'Canvas Full Width', 'canvas' ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds our template to the page dropdown
|
||||
*
|
||||
* @param array $posts_templates The posts templates.
|
||||
*/
|
||||
public function add_new_template( $posts_templates ) {
|
||||
$posts_templates = array_merge( $posts_templates, $this->templates );
|
||||
|
||||
return $posts_templates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds our template to the pages cache in order to trick WordPress
|
||||
* into thinking the template file exists where it doens't really exist.
|
||||
*
|
||||
* @param array $atts The attributes for the page attributes dropdown.
|
||||
*/
|
||||
public function register_project_templates( $atts ) {
|
||||
|
||||
// Create the key used for the themes cache.
|
||||
$cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );
|
||||
|
||||
// Retrieve the cache list. If it doesn't exist, or it's empty prepare an array.
|
||||
$templates = wp_cache_get( $cache_key, 'themes' );
|
||||
|
||||
if ( empty( $templates ) ) {
|
||||
$templates = array();
|
||||
}
|
||||
|
||||
// Since we've updated the cache, we need to delete the old cache.
|
||||
wp_cache_delete( $cache_key, 'themes' );
|
||||
|
||||
/**Now add our template to the list of templates by merging our templates
|
||||
with the existing templates array from the cache. */
|
||||
$templates = array_merge( $templates, $this->templates );
|
||||
|
||||
// Add the modified cache to allow WordPress to pick it up for listing available templates.
|
||||
wp_cache_add( $cache_key, $templates, 'themes', 1800 );
|
||||
|
||||
return $atts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the template is assigned to the page
|
||||
*
|
||||
* @param string $template The name of template.
|
||||
*/
|
||||
public function view_project_template( $template ) {
|
||||
global $post;
|
||||
|
||||
// Determines whether the query is for an existing single page.
|
||||
if ( ! is_page() ) {
|
||||
return $template;
|
||||
}
|
||||
|
||||
// If no posts found, return to avoid "Trying to get property of non-object" error.
|
||||
if ( ! isset( $post ) ) {
|
||||
return $template;
|
||||
}
|
||||
|
||||
if ( ! isset( $this->templates[ get_post_meta( $post->ID, '_wp_page_template', true ) ] ) ) {
|
||||
return $template;
|
||||
}
|
||||
|
||||
$file = CNVS_PATH . 'page-templates/' . get_post_meta( $post->ID, '_wp_page_template', true );
|
||||
|
||||
// Just to be safe, we check if the file exist first.
|
||||
if ( file_exists( $file ) ) {
|
||||
return $file;
|
||||
}
|
||||
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
|
||||
new CNVS_Page_Templates();
|
||||
@@ -0,0 +1,297 @@
|
||||
<?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 Canvas
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'CNVS' ) ) {
|
||||
/**
|
||||
* Main Core Class
|
||||
*/
|
||||
class CNVS {
|
||||
/**
|
||||
* 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( CNVS_PATH . '/canvas.php' );
|
||||
|
||||
// Vars.
|
||||
$this->version = $plugin_data['Version'];
|
||||
|
||||
// Settings.
|
||||
$this->settings = array(
|
||||
'name' => esc_html__( 'Canvas', 'canvas' ),
|
||||
'version' => $plugin_data['Version'],
|
||||
'documentation' => $plugin_data['AuthorURI'] . '/documentation/canvas',
|
||||
);
|
||||
|
||||
// Constants.
|
||||
$this->define( 'CANVAS', true );
|
||||
$this->define( 'CANVAS_VERSION', $plugin_data['Version'] );
|
||||
|
||||
// Include core.
|
||||
require_once CNVS_PATH . 'core/core-canvas-api.php';
|
||||
require_once CNVS_PATH . 'core/core-canvas-helpers.php';
|
||||
require_once CNVS_PATH . 'core/core-canvas-filters.php';
|
||||
require_once CNVS_PATH . 'core/core-canvas-post-meta.php';
|
||||
|
||||
// Include core classes.
|
||||
require_once CNVS_PATH . 'core/class-canvas-page-templates.php';
|
||||
require_once CNVS_PATH . 'core/class-canvas-gutenberg.php';
|
||||
require_once CNVS_PATH . 'core/class-canvas-layouts.php';
|
||||
|
||||
// Gutenberg blocks.
|
||||
if ( function_exists( 'register_block_type' ) ) {
|
||||
require_once CNVS_PATH . '/components/content-formatting/class-block-heading.php';
|
||||
require_once CNVS_PATH . '/components/content-formatting/class-block-list.php';
|
||||
require_once CNVS_PATH . '/components/content-formatting/class-block-paragraph.php';
|
||||
require_once CNVS_PATH . '/components/content-formatting/class-block-separator.php';
|
||||
require_once CNVS_PATH . '/components/content-formatting/class-format-badge.php';
|
||||
require_once CNVS_PATH . '/components/basic-elements/class-block-group.php';
|
||||
require_once CNVS_PATH . '/components/basic-elements/class-block-cover.php';
|
||||
require_once CNVS_PATH . '/components/basic-elements/class-block-alert.php';
|
||||
require_once CNVS_PATH . '/components/basic-elements/class-block-progress.php';
|
||||
require_once CNVS_PATH . '/components/basic-elements/class-block-collapsibles.php';
|
||||
require_once CNVS_PATH . '/components/basic-elements/class-block-tabs.php';
|
||||
require_once CNVS_PATH . '/components/basic-elements/class-block-widgetized-area.php';
|
||||
require_once CNVS_PATH . '/components/basic-elements/class-block-section-heading.php';
|
||||
require_once CNVS_PATH . '/components/layout-blocks/class-block-section.php';
|
||||
require_once CNVS_PATH . '/components/layout-blocks/class-block-row.php';
|
||||
require_once CNVS_PATH . '/components/posts/class-block-posts.php';
|
||||
require_once CNVS_PATH . '/components/posts/class-block-posts-sidebar.php';
|
||||
require_once CNVS_PATH . '/components/justified-gallery/class-block-justified-gallery.php';
|
||||
require_once CNVS_PATH . '/components/slider-gallery/class-block-slider-gallery.php';
|
||||
}
|
||||
|
||||
// Actions.
|
||||
add_action( 'canvas_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_action( 'wp_head', array( $this, 'wp_head' ), 5 );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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( 'canvas_db_version' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
update_option( 'canvas_db_version', cnvs_raw_setting( 'version' ), true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check current version
|
||||
*/
|
||||
public function check_version() {
|
||||
|
||||
// Version Data.
|
||||
$new = cnvs_raw_setting( 'version' );
|
||||
|
||||
// Get db version.
|
||||
$current = get_option( 'canvas_db_version', $new );
|
||||
|
||||
// If versions don't match.
|
||||
if ( $current && $current !== $new ) {
|
||||
/**
|
||||
* If different versions call a special hook.
|
||||
*
|
||||
* @param string $current Current version.
|
||||
* @param string $new New version.
|
||||
*/
|
||||
do_action( 'canvas_plugin_upgrade', $current, $new );
|
||||
|
||||
update_option( 'canvas_db_version', $new, true );
|
||||
}
|
||||
|
||||
if ( $current ) {
|
||||
update_option( 'canvas_db_version', $new, true );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AMP stylesheets.
|
||||
*/
|
||||
public function amp_enqueue_styles() {
|
||||
echo file_get_contents( CNVS_PATH . 'assets/css/amp.css' ); // XSS.
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will register scripts and styles for admin dashboard.
|
||||
*
|
||||
* @param string $page Current page.
|
||||
*/
|
||||
public function admin_enqueue_scripts( $page ) {
|
||||
wp_enqueue_style( 'canvas', CNVS_URL . 'assets/css/canvas.css', array(), cnvs_get_setting( 'version' ) );
|
||||
|
||||
$default_slug = apply_filters( 'canvas_scheme_default_slug', 'default' );
|
||||
$dark_slug = apply_filters( 'canvas_scheme_dark_slug', 'dark' );
|
||||
|
||||
wp_localize_script( 'jquery-ui-core', 'canvasLocalize', array(
|
||||
'postType' => get_post_type(),
|
||||
'ajaxURL' => admin_url( 'admin-ajax.php' ),
|
||||
'schemeDefaultSlug' => 'default' === $default_slug ? '' : $default_slug,
|
||||
'schemeDarkSlug' => 'default' === $dark_slug ? '' : $dark_slug,
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will register scripts and styles
|
||||
*/
|
||||
public function wp_enqueue_scripts() {
|
||||
// Styles.
|
||||
wp_enqueue_style( 'canvas', cnvs_style( CNVS_URL . 'assets/css/canvas.css' ), array(), cnvs_get_setting( 'version' ) );
|
||||
|
||||
// Add RTL support.
|
||||
wp_style_add_data( 'canvas', 'rtl', 'replace' );
|
||||
}
|
||||
/**
|
||||
* Sets up theme defaults and registers support for various WordPress features.
|
||||
*/
|
||||
public function after_setup_theme() {
|
||||
// Register custom thumbnail sizes.
|
||||
add_image_size( 'cnvs-small', 80, 80, true );
|
||||
add_image_size( 'cnvs-thumbnail', 300, 225, true );
|
||||
|
||||
// Add editor style.
|
||||
if ( is_rtl() ) {
|
||||
add_editor_style( cnvs_style( CNVS_URL . 'assets/css/canvas-rtl.css' ) );
|
||||
} else {
|
||||
add_editor_style( cnvs_style( CNVS_URL . 'assets/css/canvas.css' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire the wp_head action.
|
||||
*/
|
||||
public function wp_head() {
|
||||
?>
|
||||
<link rel="preload" href="<?php echo esc_url( CNVS_URL . 'assets/fonts/canvas-icons.woff' ); ?>" as="font" type="font/woff" crossorigin>
|
||||
<?php
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* The main function responsible for returning the one true cnvs Instance to functions everywhere.
|
||||
* Use this function like you would a global variable, except without needing to declare the global.
|
||||
*
|
||||
* Example: <?php $cnvs = cnvs(); ?>
|
||||
*/
|
||||
function cnvs() {
|
||||
|
||||
// Globals.
|
||||
global $cnvs_instance;
|
||||
|
||||
// Init.
|
||||
if ( ! isset( $cnvs_instance ) ) {
|
||||
$cnvs_instance = new CNVS();
|
||||
$cnvs_instance->init();
|
||||
}
|
||||
|
||||
return $cnvs_instance;
|
||||
}
|
||||
|
||||
// Initialize.
|
||||
cnvs();
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
/**
|
||||
* The api functions for the plugin
|
||||
*
|
||||
* @package Canvas
|
||||
*/
|
||||
|
||||
/**
|
||||
* This function will return true for a non empty array
|
||||
*
|
||||
* @param array $array Array.
|
||||
*/
|
||||
function canvas_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 canvas_is_empty( $value ) {
|
||||
return ( empty( $value ) && ! is_numeric( $value ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of cnvs()->has_setting()
|
||||
*
|
||||
* @param string $name The name.
|
||||
*/
|
||||
function cnvs_has_setting( $name = '' ) {
|
||||
return cnvs()->has_setting( $name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of cnvs()->get_setting()
|
||||
*
|
||||
* @param string $name The name.
|
||||
*/
|
||||
function cnvs_raw_setting( $name = '' ) {
|
||||
return cnvs()->get_setting( $name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of cnvs()->update_setting()
|
||||
*
|
||||
* @param string $name The name.
|
||||
* @param mixed $value The value.
|
||||
*/
|
||||
function cnvs_update_setting( $name, $value ) {
|
||||
|
||||
return cnvs()->update_setting( $name, $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of cnvs()->get_setting()
|
||||
*
|
||||
* @param string $name The name.
|
||||
* @param mixed $value The value.
|
||||
*/
|
||||
function cnvs_get_setting( $name, $value = null ) {
|
||||
|
||||
// Check settings.
|
||||
if ( cnvs_has_setting( $name ) ) {
|
||||
$value = cnvs_raw_setting( $name );
|
||||
}
|
||||
|
||||
// Filter.
|
||||
$value = apply_filters( "canvas_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 cnvs_append_setting( $name, $value ) {
|
||||
|
||||
// Vars.
|
||||
$setting = cnvs_raw_setting( $name );
|
||||
|
||||
// Bail ealry if not array.
|
||||
if ( ! is_array( $setting ) ) {
|
||||
$setting = array();
|
||||
}
|
||||
|
||||
// Append.
|
||||
$setting[] = $value;
|
||||
|
||||
// Update.
|
||||
return cnvs_update_setting( $name, $setting );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns data.
|
||||
*
|
||||
* @param string $name The name.
|
||||
*/
|
||||
function cnvs_get_data( $name ) {
|
||||
return cnvs()->get_data( $name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets data.
|
||||
*
|
||||
* @param string $name The name.
|
||||
* @param mixed $value The value.
|
||||
*/
|
||||
function cnvs_set_data( $name, $value ) {
|
||||
return cnvs()->set_data( $name, $value );
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
/**
|
||||
* The basic filters functions
|
||||
*
|
||||
* @package Canvas
|
||||
*/
|
||||
|
||||
/**
|
||||
* Powerkit PinIt disable
|
||||
*
|
||||
* @param string $selectors List selectors.
|
||||
*/
|
||||
function cnvs_powerkit_pinit_disable( $selectors ) {
|
||||
$selectors[] = '.cnvs-block-row';
|
||||
$selectors[] = '.cnvs-block-section';
|
||||
$selectors[] = '.cnvs-block-posts .entry-thumbnail';
|
||||
$selectors[] = '.cnvs-post-thumbnail';
|
||||
|
||||
return $selectors;
|
||||
}
|
||||
add_filter( 'powerkit_pinit_exclude_selectors', 'cnvs_powerkit_pinit_disable' );
|
||||
@@ -0,0 +1,736 @@
|
||||
<?php
|
||||
/**
|
||||
* The basic helpers functions
|
||||
*
|
||||
* @package Canvas
|
||||
*/
|
||||
|
||||
/**
|
||||
* Processing path of style.
|
||||
*
|
||||
* @param string $path URL to the stylesheet.
|
||||
*/
|
||||
function cnvs_style( $path ) {
|
||||
// Check RTL.
|
||||
if ( is_rtl() ) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
// Check Dev.
|
||||
$dev = CNVS_PATH . 'assets/css/canvas-dev.css';
|
||||
|
||||
if ( file_exists( $dev ) ) {
|
||||
return str_replace( '.css', '-dev.css', $path );
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
|
||||
if ( ! function_exists( 'cnvs_powerkit_module_enabled' ) ) {
|
||||
/**
|
||||
* Helper function to check the status of powerkit modules
|
||||
*
|
||||
* @param array $name Name of module.
|
||||
*/
|
||||
function cnvs_powerkit_module_enabled( $name ) {
|
||||
if ( function_exists( 'powerkit_module_enabled' ) && powerkit_module_enabled( $name ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'cnvs_post_views_enabled' ) ) {
|
||||
/**
|
||||
* Check post views module.
|
||||
*
|
||||
* @return string Type.
|
||||
*/
|
||||
function cnvs_post_views_enabled() {
|
||||
|
||||
// Post Views Counter.
|
||||
if ( class_exists( 'Post_Views_Counter' ) ) {
|
||||
return 'post_views';
|
||||
}
|
||||
|
||||
// Powerkit Post Views.
|
||||
if ( cnvs_powerkit_module_enabled( 'post_views' ) ) {
|
||||
return 'pk_post_views';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Output error message.
|
||||
*
|
||||
* @param string $message The error message.
|
||||
*/
|
||||
function cnvs_alert_warning( $message ) {
|
||||
if ( current_user_can( 'editor' ) || current_user_can( 'administrator' ) ) {
|
||||
?>
|
||||
<p class="cnvs-alert cnvs-alert-warning" role="alert">
|
||||
<?php echo wp_kses( $message, 'post' ); ?>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'cnvs_get_round_number' ) ) {
|
||||
/**
|
||||
* 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 cnvs_get_round_number( $number, $min_value = 1000, $decimal = 1 ) {
|
||||
if ( $number < $min_value ) {
|
||||
return number_format_i18n( $number );
|
||||
}
|
||||
$alphabets = array(
|
||||
1000000000 => 'B',
|
||||
1000000 => 'M',
|
||||
1000 => 'K',
|
||||
);
|
||||
foreach ( $alphabets as $key => $value ) {
|
||||
if ( $number >= $key ) {
|
||||
return round( $number / $key, $decimal ) . $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 cnvs_str_truncate( $string, $length = 80, $etc = '…', $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 cnvs_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', 'canvas' ), // Billion.
|
||||
1000000 => esc_html__( 'M', 'canvas' ), // Million.
|
||||
1000 => esc_html__( 'K', 'canvas' ), // 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 cnvs_timing_ago( $time ) {
|
||||
$periods = array( esc_html__( 'second', 'canvas' ), esc_html__( 'minute', 'canvas' ), esc_html__( 'hour', 'canvas' ), esc_html__( 'day', 'canvas' ), esc_html__( 'week', 'canvas' ), esc_html__( 'month', 'canvas' ), esc_html__( 'year', 'canvas' ), esc_html__( 'decade', 'canvas' ) );
|
||||
$lengths = array( '60', '60', '24', '7', '4.35', '12', '10' );
|
||||
|
||||
$now = time();
|
||||
|
||||
$difference = $now - $time;
|
||||
$tense = esc_html__( 'ago', 'canvas' );
|
||||
|
||||
$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 cnvs_encode_data( $content, $secret_key = 'canvas' ) {
|
||||
|
||||
$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 cnvs_decode_data( $content, $secret_key = 'canvas' ) {
|
||||
|
||||
$content = base64_decode( $content );
|
||||
|
||||
return json_decode( $content );
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt data
|
||||
*
|
||||
* @param mixed $content The content.
|
||||
* @param string $secret_key The key.
|
||||
* @return string
|
||||
*/
|
||||
function cnvs_encrypt_data( $content, $secret_key = 'canvas' ) {
|
||||
|
||||
$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 cnvs_decrypt_data( $content, $secret_key = 'canvas' ) {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user uuid
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function cnvs_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 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve paginated link for archive post pages.
|
||||
*
|
||||
* @param string|array $args Array or string of arguments for generating paginated links for archives.
|
||||
*/
|
||||
function cnvs_paginate_links( $args = '' ) {
|
||||
global $wp_query, $wp_rewrite;
|
||||
|
||||
// Setting up default values based on the current URL.
|
||||
$pagenum_link = html_entity_decode( get_pagenum_link() );
|
||||
$url_parts = explode( '?', $pagenum_link );
|
||||
|
||||
// Get max pages and current page out of the current query, if available.
|
||||
$total = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1;
|
||||
$current = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
|
||||
|
||||
// Append the format placeholder to the base URL.
|
||||
$pagenum_link = trailingslashit( $url_parts[0] ) . '%_%';
|
||||
|
||||
// URL base depends on permalink settings.
|
||||
$format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
|
||||
$format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%';
|
||||
|
||||
$defaults = array(
|
||||
'base' => $pagenum_link,
|
||||
'format' => $format,
|
||||
'total' => $total,
|
||||
'current' => $current,
|
||||
'aria_current' => 'page',
|
||||
'show_all' => false,
|
||||
'prev_next' => true,
|
||||
'prev_text' => esc_html__( '« Previous', 'canvas' ),
|
||||
'next_text' => esc_html__( 'Next »', 'canvas' ),
|
||||
'end_size' => 1,
|
||||
'mid_size' => 2,
|
||||
'type' => 'plain',
|
||||
'add_args' => array(),
|
||||
'add_fragment' => '',
|
||||
'before_page_number' => '',
|
||||
'after_page_number' => '',
|
||||
'merge_query_vars' => true,
|
||||
);
|
||||
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
|
||||
if ( ! is_array( $args['add_args'] ) ) {
|
||||
$args['add_args'] = array();
|
||||
}
|
||||
|
||||
// Merge additional query vars found in the original URL into 'add_args' array.
|
||||
if ( isset( $url_parts[1] ) && $args['merge_query_vars'] ) {
|
||||
// Find the format argument.
|
||||
$format = explode( '?', str_replace( '%_%', $args['format'], $args['base'] ) );
|
||||
$format_query = isset( $format[1] ) ? $format[1] : '';
|
||||
wp_parse_str( $format_query, $format_args );
|
||||
|
||||
// Find the query args of the requested URL.
|
||||
wp_parse_str( $url_parts[1], $url_query_args );
|
||||
|
||||
// Remove the format argument from the array of query arguments, to avoid overwriting custom format.
|
||||
foreach ( $format_args as $format_arg => $format_arg_value ) {
|
||||
unset( $url_query_args[ $format_arg ] );
|
||||
}
|
||||
|
||||
$args['add_args'] = array_merge( $args['add_args'], urlencode_deep( $url_query_args ) );
|
||||
}
|
||||
|
||||
// Who knows what else people pass in $args.
|
||||
$total = (int) $args['total'];
|
||||
if ( $total < 2 ) {
|
||||
return;
|
||||
}
|
||||
$current = (int) $args['current'];
|
||||
$end_size = (int) $args['end_size'];
|
||||
if ( $end_size < 1 ) {
|
||||
$end_size = 1;
|
||||
}
|
||||
$mid_size = (int) $args['mid_size'];
|
||||
if ( $mid_size < 0 ) {
|
||||
$mid_size = 2;
|
||||
}
|
||||
$add_args = $args['add_args'];
|
||||
$r = '';
|
||||
$page_links = array();
|
||||
$dots = false;
|
||||
|
||||
if ( $args['prev_next'] && $current && 1 < $current ) :
|
||||
$link = str_replace( '%_%', 2 === $current ? '' : $args['format'], $args['base'] );
|
||||
$link = str_replace( '%#%', $current - 1, $link );
|
||||
if ( $add_args ) {
|
||||
$link = add_query_arg( $add_args, $link );
|
||||
}
|
||||
$link .= $args['add_fragment'];
|
||||
|
||||
/**
|
||||
* Filters the paginated links for the given archive pages.
|
||||
*
|
||||
* @param string $link The paginated link URL.
|
||||
*/
|
||||
$page_links[] = '<a class="prev page-numbers" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $args['prev_text'] . '</a>';
|
||||
endif;
|
||||
for ( $n = 1; $n <= $total; $n++ ) :
|
||||
if ( $n === $current ) :
|
||||
$page_links[] = "<span aria-current='" . esc_attr( $args['aria_current'] ) . "' class='page-numbers current'>" . $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number'] . '</span>';
|
||||
$dots = true;
|
||||
else :
|
||||
if ( $args['show_all'] || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
|
||||
$link = str_replace( '%_%', 1 === $n ? '' : $args['format'], $args['base'] );
|
||||
$link = str_replace( '%#%', $n, $link );
|
||||
if ( $add_args ) {
|
||||
$link = add_query_arg( $add_args, $link );
|
||||
}
|
||||
$link .= $args['add_fragment'];
|
||||
|
||||
/** This filter is documented in wp-includes/general-template.php */
|
||||
$page_links[] = "<a class='page-numbers' href='" . esc_url( apply_filters( 'paginate_links', $link ) ) . "'>" . $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number'] . '</a>';
|
||||
$dots = true;
|
||||
elseif ( $dots && ! $args['show_all'] ) :
|
||||
$page_links[] = '<span class="page-numbers dots">' . esc_html__( '…', 'canvas' ) . '</span>';
|
||||
$dots = false;
|
||||
endif;
|
||||
endif;
|
||||
endfor;
|
||||
if ( $args['prev_next'] && $current && $current < $total ) :
|
||||
$link = str_replace( '%_%', $args['format'], $args['base'] );
|
||||
$link = str_replace( '%#%', $current + 1, $link );
|
||||
if ( $add_args ) {
|
||||
$link = add_query_arg( $add_args, $link );
|
||||
}
|
||||
$link .= $args['add_fragment'];
|
||||
|
||||
/** This filter is documented in wp-includes/general-template.php */
|
||||
$page_links[] = '<a class="next page-numbers" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $args['next_text'] . '</a>';
|
||||
endif;
|
||||
switch ( $args['type'] ) {
|
||||
case 'array':
|
||||
return $page_links;
|
||||
|
||||
case 'list':
|
||||
$r .= "<ul class='page-numbers'>\n\t<li>";
|
||||
$r .= join( "</li>\n\t<li>", $page_links );
|
||||
$r .= "</li>\n</ul>\n";
|
||||
break;
|
||||
|
||||
default:
|
||||
$r = join( "\n", $page_links );
|
||||
break;
|
||||
}
|
||||
return $r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get blog posts page URL.
|
||||
*
|
||||
* @param array $attributes The attributes.
|
||||
*/
|
||||
function cnvs_get_block_posts_page_url( $attributes ) {
|
||||
global $wp_rewrite;
|
||||
|
||||
// The front page IS the posts page. Get its URL.
|
||||
$url = get_home_url();
|
||||
|
||||
// If front page is set to display a static page, get the URL of the posts page.
|
||||
if ( 'page' === get_option( 'show_on_front' ) ) {
|
||||
$url = null;
|
||||
|
||||
if ( get_option( 'page_for_posts' ) ) {
|
||||
$url = get_permalink( get_option( 'page_for_posts' ) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $attributes['query']['categories'] ) && $attributes['query']['categories'] ) {
|
||||
|
||||
$url = get_term_link( $attributes['query']['categories'], 'category' );
|
||||
}
|
||||
|
||||
if ( isset( $attributes['query']['tags'] ) && $attributes['query']['tags'] ) {
|
||||
|
||||
$url = get_term_link( $attributes['query']['tags'], 'post_tag' );
|
||||
}
|
||||
|
||||
if ( $url ) {
|
||||
if ( $wp_rewrite->using_permalinks() ) {
|
||||
$url = untrailingslashit( $url ) . '/page/%#%';
|
||||
} else {
|
||||
$url = add_query_arg( array( 'paged' => '%#%' ), untrailingslashit( $url ) );
|
||||
}
|
||||
|
||||
return user_trailingslashit( $url );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the available image sizes
|
||||
*/
|
||||
function cnvs_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 cnvs_get_image_size( $size ) {
|
||||
if ( ! is_string( $size ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sizes = cnvs_get_available_image_sizes();
|
||||
|
||||
return isset( $sizes[ $size ] ) ? $sizes[ $size ] : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list available image sizes
|
||||
*/
|
||||
function cnvs_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 = cnvs_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( 'canvas_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 cnvs_get_gutenberg_button_fields( $field_prefix = 'button', $section_name = '', $active_callback = array() ) {
|
||||
|
||||
$fields = array(
|
||||
array(
|
||||
'key' => $field_prefix . 'Style',
|
||||
'label' => esc_html__( 'Style', 'canvas' ),
|
||||
'section' => $section_name,
|
||||
'type' => 'select',
|
||||
'default' => '',
|
||||
'choices' => array(
|
||||
'' => esc_html__( 'Default', 'canvas' ),
|
||||
'outline' => esc_html__( 'Outline', 'canvas' ),
|
||||
'squared' => esc_html__( 'Squared', 'canvas' ),
|
||||
),
|
||||
'active_callback' => $active_callback,
|
||||
),
|
||||
array(
|
||||
'key' => $field_prefix . 'Size',
|
||||
'label' => esc_html__( 'Size', 'canvas' ),
|
||||
'section' => $section_name,
|
||||
'type' => 'select',
|
||||
'default' => '',
|
||||
'choices' => array(
|
||||
'' => esc_html__( 'Default', 'canvas' ),
|
||||
'sm' => esc_html__( 'Small', 'canvas' ),
|
||||
'lg' => esc_html__( 'Large', 'canvas' ),
|
||||
),
|
||||
'active_callback' => $active_callback,
|
||||
),
|
||||
array(
|
||||
'key' => $field_prefix . 'FullWidth',
|
||||
'label' => esc_html__( 'Full Width', 'canvas' ),
|
||||
'section' => $section_name,
|
||||
'type' => 'toggle',
|
||||
'default' => false,
|
||||
'active_callback' => $active_callback,
|
||||
),
|
||||
array(
|
||||
'key' => $field_prefix . 'ColorBg',
|
||||
'label' => esc_html__( 'Background Color', 'canvas' ),
|
||||
'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', 'canvas' ),
|
||||
'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', 'canvas' ),
|
||||
'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', 'canvas' ),
|
||||
'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 cnvs_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-cnvs-button-size-' . $attributes[ $field_prefix . 'Size' ];
|
||||
}
|
||||
|
||||
// FullWidth.
|
||||
if ( isset( $attributes[ $field_prefix . 'FullWidth' ] ) && $attributes[ $field_prefix . 'FullWidth' ] ) {
|
||||
$class_name .= ' is-cnvs-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,355 @@
|
||||
<?php
|
||||
/**
|
||||
* Post Meta Helper Functions
|
||||
*
|
||||
* These helper functions return post meta.
|
||||
*
|
||||
* @package Canvas
|
||||
* @subpackage Core
|
||||
*/
|
||||
|
||||
if ( ! function_exists( 'cnvs_allowed_post_meta' ) ) {
|
||||
/**
|
||||
* Allowed Post Meta
|
||||
*
|
||||
* @param bool $compact If compact version shall be displayed.
|
||||
* @param array $exclude Exclude list.
|
||||
*/
|
||||
function cnvs_allowed_post_meta( $compact = false, $exclude = array() ) {
|
||||
$allowed = array(
|
||||
'category' => esc_html__( 'Category', 'canvas' ),
|
||||
'date' => esc_html__( 'Date', 'canvas' ),
|
||||
'author' => esc_html__( 'Author', 'canvas' ),
|
||||
'comments' => esc_html__( 'Comments count', 'canvas' ),
|
||||
);
|
||||
|
||||
if ( cnvs_post_views_enabled() ) {
|
||||
$allowed['views'] = esc_html__( 'Views', 'canvas' );
|
||||
}
|
||||
|
||||
if ( cnvs_powerkit_module_enabled( 'reading_time' ) ) {
|
||||
$allowed['reading_time'] = esc_html__( 'Reading time', 'canvas' );
|
||||
}
|
||||
|
||||
$allowed = apply_filters( 'canvas_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( 'cnvs_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 cnvs_block_post_meta( $settings, $meta, $echo = true, $compact = false ) {
|
||||
|
||||
$func_handler = apply_filters( 'canvas_get_block_post_meta_handler', false );
|
||||
|
||||
if ( $func_handler ) {
|
||||
return call_user_func( $func_handler, $settings, $meta, $echo, $compact );
|
||||
}
|
||||
|
||||
$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['metaCompact'] ) && $settings['metaCompact'] ) {
|
||||
$compact = true;
|
||||
}
|
||||
|
||||
$allowed = apply_filters( 'canvas_allowed_block_post_meta', $allowed, $settings, $meta, $echo, $compact );
|
||||
|
||||
if ( ! $allowed ) {
|
||||
return;
|
||||
}
|
||||
|
||||
return cnvs_get_post_meta( $meta, $compact, $echo, $allowed );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'cnvs_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 cnvs_get_post_meta( $meta, $compact = false, $echo = true, $allowed = array() ) {
|
||||
|
||||
$func_handler = apply_filters( 'canvas_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 = cnvs_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="cnvs-post-meta post-meta">';
|
||||
|
||||
// Add normal meta types to the list.
|
||||
foreach ( $meta as $type ) {
|
||||
$function = "cnvs_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 = "cnvs_get_meta_$meta";
|
||||
|
||||
if ( function_exists( $function ) ) {
|
||||
$output .= $function( 'div', $compact );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( $echo ) {
|
||||
echo (string) $output; // XSS ok.
|
||||
} else {
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'cnvs_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 cnvs_get_meta_date( $tag = 'span', $compact = false ) {
|
||||
|
||||
$output = '<' . esc_html( $tag ) . ' class="cnvs-meta-date meta-date">';
|
||||
|
||||
if ( false === $compact ) {
|
||||
$time_string = get_the_date();
|
||||
} else {
|
||||
$time_string = get_the_date( 'd.m.y' );
|
||||
}
|
||||
|
||||
$output .= apply_filters( 'canvas_post_meta_date_output', '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>' );
|
||||
|
||||
$output .= '</' . esc_html( $tag ) . '>';
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'cnvs_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 cnvs_get_meta_author( $tag = 'span', $compact = false ) {
|
||||
|
||||
$authors = array( get_the_author_meta( 'ID' ) );
|
||||
|
||||
$output = '<' . esc_html( $tag ) . ' class="cnvs-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', 'canvas' ), get_the_author() ) ),
|
||||
// Author.
|
||||
get_the_author()
|
||||
);
|
||||
|
||||
$output .= '</' . esc_html( $tag ) . '>';
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'cnvs_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 cnvs_get_meta_category( $tag = 'span', $compact = false, $post_id = null ) {
|
||||
|
||||
if ( ! $post_id ) {
|
||||
$post_id = get_the_ID();
|
||||
}
|
||||
|
||||
$output = '<' . esc_html( $tag ) . ' class="cnvs-meta-category meta-category">';
|
||||
|
||||
$output .= get_the_category_list( '', '', $post_id );
|
||||
|
||||
$output .= '</' . esc_html( $tag ) . '>';
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'cnvs_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 cnvs_get_meta_comments( $tag = 'span', $compact = false ) {
|
||||
$output = '';
|
||||
|
||||
$output .= '<' . esc_html( $tag ) . ' class="cnvs-meta-comments meta-comments">';
|
||||
|
||||
if ( true === $compact ) {
|
||||
$output .= '<i class="cnvs-icon cnvs-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', 'canvas' ), esc_html__( 'One comment', 'canvas' ), '% ' . esc_html__( 'comments', 'canvas' ), 'comments-link', '' );
|
||||
$output .= ob_get_clean();
|
||||
}
|
||||
|
||||
$output .= '</' . esc_html( $tag ) . '>';
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'cnvs_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 cnvs_get_meta_reading_time( $tag = 'span', $compact = false ) {
|
||||
|
||||
if ( ! cnvs_powerkit_module_enabled( 'reading_time' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$reading_time = powerkit_get_post_reading_time();
|
||||
|
||||
$output = '<' . esc_html( $tag ) . ' class="cnvs-meta-reading-time meta-reading-time">';
|
||||
|
||||
if ( true === $compact ) {
|
||||
$output .= '<i class="cnvs-icon cnvs-icon-watch"></i>' . intval( $reading_time ) . ' ' . esc_html( 'min', 'canvas' );
|
||||
} else {
|
||||
/* translators: %s number of minutes */
|
||||
$output .= esc_html( sprintf( _n( '%s minute read', '%s minute read', $reading_time, 'canvas' ), $reading_time ) );
|
||||
}
|
||||
|
||||
$output .= '</' . esc_html( $tag ) . '>';
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'cnvs_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 cnvs_get_meta_views( $tag = 'span', $compact = false ) {
|
||||
|
||||
switch ( cnvs_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( 'canvas_post_minimum_views', 1 ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$output = '<' . esc_html( $tag ) . ' class="cnvs-meta-views meta-views">';
|
||||
|
||||
$views_rounded = cnvs_get_round_number( $views );
|
||||
|
||||
if ( true === $compact ) {
|
||||
$output .= '<i class="cnvs-icon cnvs-icon-eye"></i>' . esc_html( $views_rounded );
|
||||
} else {
|
||||
/* translators: %s number of post views */
|
||||
$output .= esc_html( sprintf( _n( '%s view', '%s views', $views, 'canvas' ), $views_rounded ) );
|
||||
}
|
||||
|
||||
$output .= '</' . esc_html( $tag ) . '>';
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user