view_script_handles = array();
}
wp_localize_script(
'weboptics-advance-post-grid-editor-script',
'woApg',
array(
'placeholderUrl' => plugin_dir_url( __FILE__ ) . 'build/assets/images/placeholder.svg',
)
);
}
/**
* Enqueue view script with jQuery dependency.
*/
public function enqueue_view_script() {
if ( ! has_block( 'weboptics/advance-post-grid' ) ) {
return;
}
$asset_file = include __DIR__ . '/build/view.asset.php';
wp_enqueue_script(
'advance-post-grid-view',
plugin_dir_url( __FILE__ ) . 'build/view.js',
array_merge( $asset_file['dependencies'], array( 'jquery' ) ),
$asset_file['version'],
true
);
}
/**
* Allow the "Order By" options used by the block editor preview (Menu Order,
* Random, Comment Count) that the REST API rejects by default. WP_Query already
* supports these on the frontend, so this keeps the editor preview in sync.
*/
public function register_rest_orderby_options() {
$wo_apg_post_types = get_post_types( array( 'show_in_rest' => true ), 'names' );
foreach ( $wo_apg_post_types as $wo_apg_post_type ) {
add_filter( "rest_{$wo_apg_post_type}_collection_params", array( $this, 'add_rest_orderby_options' ) );
}
}
/**
* Add extra orderby choices to a post type's REST collection params.
*
* @param array $query_params Collection parameters for the post type's REST controller.
*/
public function add_rest_orderby_options( $query_params ) {
$wo_apg_extra_orderby = array( 'rand', 'comment_count', 'menu_order' );
foreach ( $wo_apg_extra_orderby as $wo_apg_orderby_option ) {
if ( ! in_array( $wo_apg_orderby_option, $query_params['orderby']['enum'], true ) ) {
$query_params['orderby']['enum'][] = $wo_apg_orderby_option;
}
}
return $query_params;
}
/**
* Calculate Read Time
*
* @param string $content content string.
*/
public static function calculate_read_time( $content ) {
$word_count = str_word_count( strip_tags( $content ) );
$words_per_minute = 200;
$read_time = ceil( $word_count / $words_per_minute );
return $read_time;
}
/**
* Pagination Function
*
* The pagination function to display pagination on any archive page
*
* @param number $pages are total number of pages.
* @param array $args is a array of pagination settings.
*
* @return void|string
*/
public static function pagination( $pages = '', $args = array() ) {
$showitems = $args['showitems'] ?? 5;
$first_last = $args['first_last'] ?? true;
$prev_next = $args['prev_next'] ?? true;
$show_range = $args['show_range'] ?? true;
$has_dotted = $args['has_dotted'] ?? false;
$is_ajax = $args['is_ajax'] ?? false;
$class_name = $args['class_name'] ?? '';
$ajax_paged = $args['paged'] ?? 1;
$pagination_unique_classes = 'pagination-' . $class_name;
if ( true === $is_ajax ) {
$paged = $ajax_paged;
} else {
$paged = $ajax_paged;
}
if ( '' === $pages ) {
global $wp_query;
$pages = $wp_query->max_num_pages;
if ( ! $pages ) {
$pages = 1;
}
}
if ( $is_ajax ) {
$html = '';
if ( 1 !== $pages ) {
$pagination_unique_classes .= ' ajax-pagination-' . $class_name;
$html .= '
';
}
return $html;
} else {
if ( 1 !== $pages ) {
echo '';
}
}
}
/**
* Get current page url
*/
public static function get_current_page_url() {
$protocol = ( ! empty( $_SERVER['HTTPS'] ) && 'off' !== $_SERVER['HTTPS'] ) || ( ! empty( $_SERVER['SERVER_PORT'] ) && 443 === $_SERVER['SERVER_PORT'] ) ? 'https://' : 'http://';
if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) {
$url = esc_url( wp_unslash( $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) );
return $url;
}
return null;
}
/**
* Get page number from url
*
* @param string $url page url.
*/
public static function get_page_number_from_url( $url ) {
$pattern = '/\/page\/(\d+)\//';
if ( preg_match( $pattern, $url, $matches ) ) {
return isset( $matches[1] ) ? (int) $matches[1] : null;
}
return null;
}
/**
* Shortcode to render Advance Post Grid
*
* @param array $attributes Attributes array data.
*
* Usage: [advance_post_grid postGrid="3" postType="post" postPerPage="6" PostOrder="DESC" PostOrderBy="date" showHidePagi="true" showHideLink="true" showHideCat="true" showHideRT="true" showHideDate="true"]
*/
public function shortcode_render( $attributes ) {
ob_start();
// Remap lowercase attrs back to camelCase.
$mapped_attrs = array();
foreach ( $attributes as $key => $value ) {
switch ( strtolower( $key ) ) {
case 'postgrid':
$mapped_attrs['postGrid'] = $value;
break;
case 'postgridmobile':
$mapped_attrs['postGridMobile'] = $value;
break;
case 'postgridtablet':
$mapped_attrs['postGridTablet'] = $value;
break;
case 'enablepostgridres':
$mapped_attrs['enablePostGridRes'] = filter_var( $value, FILTER_VALIDATE_BOOLEAN );
break;
case 'posttype':
$mapped_attrs['postType'] = $value;
break;
case 'postperpage':
$mapped_attrs['postPerPage'] = $value;
break;
case 'postorder':
$mapped_attrs['PostOrder'] = $value;
break;
case 'postorderby':
$mapped_attrs['PostOrderBy'] = $value;
break;
case 'showhidepagi':
$mapped_attrs['showHidePagi'] = filter_var( $value, FILTER_VALIDATE_BOOLEAN );
break;
case 'showhidelink':
$mapped_attrs['showHideLink'] = filter_var( $value, FILTER_VALIDATE_BOOLEAN );
break;
case 'showhidecat':
$mapped_attrs['showHideCat'] = filter_var( $value, FILTER_VALIDATE_BOOLEAN );
break;
case 'showhidert':
$mapped_attrs['showHideRT'] = filter_var( $value, FILTER_VALIDATE_BOOLEAN );
break;
case 'showhidedate':
$mapped_attrs['showHideDate'] = filter_var( $value, FILTER_VALIDATE_BOOLEAN );
break;
}
}
// Merge with defaults
$attributes = shortcode_atts(
array(
'postGrid' => 3,
'enablePostGridRes' => true,
'postGridTablet' => 2,
'postGridMobile' => 1,
'postType' => 'post',
'postPerPage' => 12,
'PostOrder' => 'DESC',
'PostOrderBy' => 'title',
'showHidePagi' => true,
'showHideLink' => true,
'showHideCat' => true,
'showHideRT' => true,
'showHideDate' => true,
),
$mapped_attrs,
'advance_post_grid'
);
wp_enqueue_style(
'advance-post-grid-style',
plugin_dir_url( __FILE__ ) . 'build/style-index.css',
array(),
filemtime( plugin_dir_path( __FILE__ ) . 'build/style-index.css' )
);
require_once __DIR__ . '/build/render.php';
return ob_get_clean();
}
}
new WO_APG_Init();