first commit
This commit is contained in:
55
inc/allow_svg.php
Normal file
55
inc/allow_svg.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
/** Allow SVG uploads for administrator users.
|
||||
* @param array $upload_mimes Allowed mime types.
|
||||
* @return mixed
|
||||
*/
|
||||
add_filter(
|
||||
'upload_mimes',
|
||||
function ($upload_mimes) {
|
||||
// By default, only administrator users are allowed to add SVGs.
|
||||
// To enable more user types edit or comment the lines below but beware of
|
||||
// the security risks if you allow any user to upload SVG files.
|
||||
if (!current_user_can('administrator')) {
|
||||
return $upload_mimes;
|
||||
}
|
||||
|
||||
$upload_mimes['svg'] = 'image/svg+xml';
|
||||
$upload_mimes['svgz'] = 'image/svg+xml';
|
||||
|
||||
return $upload_mimes;
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Add SVG files mime check.
|
||||
* @param array $wp_check_filetype_and_ext Values for the extension, mime type, and corrected filename.
|
||||
* @param string $file Full path to the file.
|
||||
* @param string $filename The name of the file (may differ from $file due to $file being in a tmp directory).
|
||||
* @param string[] $mimes Array of mime types keyed by their file extension regex.
|
||||
* @param string|false $real_mime The actual mime type or false if the type cannot be determined.
|
||||
*/
|
||||
add_filter(
|
||||
'wp_check_filetype_and_ext',
|
||||
function ($wp_check_filetype_and_ext, $file, $filename, $mimes, $real_mime) {
|
||||
|
||||
if (!$wp_check_filetype_and_ext['type']) {
|
||||
|
||||
$check_filetype = wp_check_filetype($filename, $mimes);
|
||||
$ext = $check_filetype['ext'];
|
||||
$type = $check_filetype['type'];
|
||||
$proper_filename = $filename;
|
||||
|
||||
if ($type && 0 === strpos($type, 'image/') && 'svg' !== $ext) {
|
||||
$ext = false;
|
||||
$type = false;
|
||||
}
|
||||
|
||||
$wp_check_filetype_and_ext = compact('ext', 'type', 'proper_filename');
|
||||
}
|
||||
|
||||
return $wp_check_filetype_and_ext;
|
||||
},
|
||||
10,
|
||||
5
|
||||
);
|
8
inc/custom.php
Normal file
8
inc/custom.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
require_once get_template_directory() . '/inc/allow_svg.php';
|
||||
require_once get_template_directory() . '/inc/required_plugins.php';
|
||||
require_once get_template_directory() . '/inc/site_member.php';
|
||||
require_once get_template_directory() . '/inc/custom_post_types.php';
|
||||
require_once get_template_directory() . '/inc/styles_and_scripts.php';
|
||||
require_once get_template_directory() . '/inc/technicalseo.php';
|
3
inc/custom_post_types.php
Normal file
3
inc/custom_post_types.php
Normal file
@@ -0,0 +1,3 @@
|
||||
<?php
|
||||
|
||||
// Custom Post Types
|
54
inc/required_plugins.php
Normal file
54
inc/required_plugins.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
// REQUIRED PLUGINS
|
||||
|
||||
require_once dirname(__FILE__) . '../../class-tgm-plugin-activation.php';
|
||||
|
||||
add_action('tgmpa_register', 'my_theme_register_required_plugins');
|
||||
|
||||
function my_theme_register_required_plugins()
|
||||
{
|
||||
/*
|
||||
* Array of plugin arrays. Required keys are name and slug.
|
||||
* If the source is NOT from the .org repo, then source is also required.
|
||||
*/
|
||||
$plugins = array(
|
||||
array(
|
||||
'name' => 'Yoast SEO',
|
||||
'slug' => 'wordpress-seo',
|
||||
'required' => false,
|
||||
)
|
||||
);
|
||||
|
||||
/*
|
||||
* Array of configuration settings. Amend each line as needed.
|
||||
*
|
||||
* TGMPA will start providing localized text strings soon. If you already have translations of our standard
|
||||
* strings available, please help us make TGMPA even better by giving us access to these translations or by
|
||||
* sending in a pull-request with .po file(s) with the translations.
|
||||
*
|
||||
* Only uncomment the strings in the config array if you want to customize the strings.
|
||||
*/
|
||||
$config = array(
|
||||
'id' => 'tgmpa', // Unique ID for hashing notices for multiple instances of TGMPA.
|
||||
'default_path' => '', // Default absolute path to bundled plugins.
|
||||
'menu' => 'tgmpa-install-plugins', // Menu slug.
|
||||
'parent_slug' => 'themes.php', // Parent menu slug.
|
||||
'capability' => 'edit_theme_options', // Capability needed to view plugin install page, should be a capability associated with the parent menu used.
|
||||
'has_notices' => true, // Show admin notices or not.
|
||||
'dismissable' => true, // If false, a user cannot dismiss the nag message.
|
||||
'dismiss_msg' => '', // If 'dismissable' is false, this message will be output at top of nag.
|
||||
'is_automatic' => false, // Automatically activate plugins after installation or not.
|
||||
'message' => '', // Message to output right before the plugins table.
|
||||
/*
|
||||
'strings' => array(
|
||||
'page_title' => __( 'Install Required Plugins', 'theme-slug' ),
|
||||
'menu_title' => __( 'Install Plugins', 'theme-slug' ),
|
||||
// <snip>...</snip>
|
||||
'nag_type' => 'updated', // Determines admin notice type - can only be 'updated', 'update-nag' or 'error'.
|
||||
)
|
||||
*/
|
||||
);
|
||||
|
||||
tgmpa($plugins, $config);
|
||||
}
|
114
inc/site_member.php
Normal file
114
inc/site_member.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
// Redirect subscriber accounts out of admin and onto homepage
|
||||
add_action('admin_init', 'redirectSubsToFrontend');
|
||||
|
||||
function redirectSubsToFrontend()
|
||||
{
|
||||
$ourCurrentUser = wp_get_current_user();
|
||||
|
||||
if (count($ourCurrentUser->roles) == 1 and $ourCurrentUser->roles[0] == 'subscriber') {
|
||||
wp_redirect(site_url('/'));
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------
|
||||
|
||||
// Remove Admin Bar For Subs
|
||||
add_action('wp_loaded', 'noSubsAdminBar');
|
||||
|
||||
function noSubsAdminBar()
|
||||
{
|
||||
$ourCurrentUser = wp_get_current_user();
|
||||
|
||||
if (count($ourCurrentUser->roles) == 1 and $ourCurrentUser->roles[0] == 'subscriber') {
|
||||
show_admin_bar(false);
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------
|
||||
|
||||
// Customize Login Screen
|
||||
add_filter('login_headerurl', 'ourHeaderUrl');
|
||||
|
||||
function ourHeaderUrl()
|
||||
{
|
||||
return esc_url(site_url('/'));
|
||||
}
|
||||
|
||||
add_action('login_enqueue_scripts', 'ourLoginCSS');
|
||||
|
||||
function ourLoginCSS()
|
||||
{
|
||||
wp_enqueue_style('login styles', get_theme_file_uri('/assets/css/styles.css'));
|
||||
}
|
||||
|
||||
add_filter('login_headertitle', 'ourLoginTitle');
|
||||
|
||||
function ourLoginTitle()
|
||||
{
|
||||
return get_bloginfo('name');
|
||||
}
|
||||
|
||||
// Logout Without Confirmation
|
||||
function getLogoutUrl($redirectUrl = '')
|
||||
{
|
||||
if (!$redirectUrl) $redirectUrl = site_url();
|
||||
$return = str_replace("&", '&', wp_logout_url($redirectUrl));
|
||||
return $return;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Bypass logout confirmation on nonce verification failure
|
||||
*/
|
||||
function logout_without_confirmation($action, $result)
|
||||
{
|
||||
if (!$result && ($action == 'log-out')) {
|
||||
wp_safe_redirect(getLogoutUrl());
|
||||
exit();
|
||||
}
|
||||
}
|
||||
add_action('check_admin_referer', 'logout_without_confirmation', 1, 2);
|
||||
|
||||
// ------------------------------------------------------------------------------------------
|
||||
|
||||
// Change Registration Message
|
||||
|
||||
function change_reg_message($message)
|
||||
{
|
||||
// change messages that contain 'Register'
|
||||
if (strpos($message, 'Register') !== FALSE) {
|
||||
$newMessage = "Register with " . get_bloginfo('name');
|
||||
return '<p class="message register">' . $newMessage . '</p>';
|
||||
} else {
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
|
||||
// add our new function to the login_message hook
|
||||
add_action('login_message', 'change_reg_message');
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------------------
|
||||
|
||||
// function custom_wp_new_user_notification_email($wp_new_user_notification_email, $user, $blogname)
|
||||
// {
|
||||
// // Customize the email subject
|
||||
// $wp_new_user_notification_email['subject'] = 'Solr Lights Media';
|
||||
|
||||
// // Customize the email message
|
||||
// $wp_new_user_notification_email['message'] = "Dear {$user->display_name},
|
||||
|
||||
// Thank you for your interest in registering with Solr Lights Media.
|
||||
|
||||
// Please visit the following url to download, fill out and complete the registration form
|
||||
|
||||
// https://google.com";
|
||||
|
||||
// // Return the modified email
|
||||
// return $wp_new_user_notification_email;
|
||||
// }
|
||||
// add_filter('wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 10, 3);
|
10
inc/styles_and_scripts.php
Normal file
10
inc/styles_and_scripts.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
function custom_block_editor_styles()
|
||||
{
|
||||
// Enqueue block editor style
|
||||
wp_enqueue_style('custom-block-editor-styles', get_template_directory_uri() . '/assets/css/tailwind_for_wp_editor.css', false, '1.0', 'all');
|
||||
|
||||
// You can add more styles or scripts here if needed
|
||||
}
|
||||
add_action('enqueue_block_editor_assets', 'custom_block_editor_styles');
|
38
inc/technicalseo.php
Normal file
38
inc/technicalseo.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
// Add Facebook Open Graph and Twitter Card to Head
|
||||
add_action('wp_head', 'open_graph_twitter_card', 2);
|
||||
|
||||
function open_graph_twitter_card()
|
||||
{
|
||||
// Get the post or page ID
|
||||
$post_id = get_the_ID();
|
||||
|
||||
// Get the post or page title
|
||||
$title = get_the_title($post_id);
|
||||
|
||||
// Get the post or page excerpt (description)
|
||||
$description = (get_the_excerpt()) ? get_the_excerpt() : get_bloginfo('description');
|
||||
|
||||
// Get the post or page URL
|
||||
$url = get_permalink($post_id);
|
||||
|
||||
// Set default image url
|
||||
$default_image_url = get_theme_file_uri('/screenshot.png');
|
||||
|
||||
// Get the post thumbnail (featured image) URL
|
||||
$thumbnail = get_the_post_thumbnail_url($post_id);
|
||||
?>
|
||||
<!-- Open Graph meta tags -->
|
||||
<meta property="og:title" content="<?php echo $title . ' - ' . get_bloginfo('name'); ?>" />
|
||||
<meta property="og:description" content="<?php echo $description ?>" />
|
||||
<meta property="og:url" content="<?php echo $url ?>" />
|
||||
<meta property="og:image" content="<?php echo $default_image_url ?>" />
|
||||
<!-- Twitter Card meta tags -->
|
||||
<meta name="twitter:card" content="summary" />
|
||||
<meta name="twitter:title" content="<?php echo $title . ' - ' . get_bloginfo('name'); ?>" />
|
||||
<meta name="twitter:description" content="<?php echo $description ?>" />
|
||||
<meta name="twitter:image" content="<?php echo $default_image_url ?>" />
|
||||
<?php
|
||||
|
||||
}
|
277
inc/wp_pg_blocks_helpers.php
Normal file
277
inc/wp_pg_blocks_helpers.php
Normal file
@@ -0,0 +1,277 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Name: PG_Blocks
|
||||
* GitHub URI:
|
||||
* Description:
|
||||
* Version: 1.0
|
||||
* Author: Matjaz Trontelj - @pinegrow
|
||||
* License: GPL-2.0+
|
||||
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
|
||||
*
|
||||
*/
|
||||
|
||||
if(! class_exists( 'PG_Blocks_v2' )) {
|
||||
class PG_Blocks_v2
|
||||
{
|
||||
|
||||
static $helpers_registered = false;
|
||||
|
||||
static $recursive_level = 0;
|
||||
|
||||
static $controls_version = '2';
|
||||
|
||||
static function register_block_type($reg_args)
|
||||
{
|
||||
if(empty($reg_args[ 'base_url' ])) {
|
||||
$reg_args[ 'base_url' ] = get_template_directory_uri();
|
||||
}
|
||||
|
||||
if(empty($reg_args[ 'base_path' ])) {
|
||||
$reg_args[ 'base_path' ] = get_template_directory();
|
||||
}
|
||||
|
||||
if(empty($reg_args[ 'version' ])) {
|
||||
$reg_args[ 'version' ] = false;
|
||||
}
|
||||
|
||||
$base_url = trailingslashit($reg_args[ 'base_url' ] );
|
||||
|
||||
if (!self::$helpers_registered) {
|
||||
self::$helpers_registered = true;
|
||||
|
||||
wp_register_script("pg-blocks-controls-" . self::$controls_version,
|
||||
$base_url . 'blocks/pg-blocks-controls-' . self::$controls_version . '.js',
|
||||
array('wp-blocks', 'wp-block-editor', 'wp-server-side-render', 'wp-media-utils', 'wp-data', 'wp-element'), $reg_args[ 'version' ]);
|
||||
|
||||
wp_register_style('pg-blocks-controls-style-' . self::$controls_version,
|
||||
$base_url . 'blocks/pg-blocks-controls-' . self::$controls_version . '.css',
|
||||
array(), $reg_args[ 'version' ]);
|
||||
}
|
||||
|
||||
$handle_prefix = 'block-' . str_replace('/', '-', $reg_args['name']);
|
||||
$editor_script_handle = $handle_prefix . '-script';
|
||||
$just_name = basename($reg_args['name']);
|
||||
$slug = dirname($reg_args['name']);
|
||||
|
||||
$style_handle = null;
|
||||
$script_handle = null;
|
||||
$view_script_handle = null;
|
||||
|
||||
if (!empty($reg_args['enqueue_style'])) {
|
||||
$style_handle = 'block-' . md5($reg_args['enqueue_style']);
|
||||
wp_register_style($style_handle, $reg_args['enqueue_style'], array(), $reg_args[ 'version' ]);
|
||||
}
|
||||
|
||||
$editor_style_handle = 'pg-blocks-controls-style-' . self::$controls_version;
|
||||
|
||||
if (!empty($reg_args['enqueue_editor_style'])) {
|
||||
$editor_style_handle = 'block-editor-' . md5($reg_args['enqueue_editor_style']);
|
||||
wp_register_style($editor_style_handle, $reg_args['enqueue_editor_style'], array('pg-blocks-controls-style-' . self::$controls_version), $reg_args[ 'version' ]);
|
||||
}
|
||||
|
||||
$script_dependencies = array('wp-blocks', 'wp-block-editor', 'wp-server-side-render', 'wp-media-utils', 'wp-data', 'wp-element', 'pg-blocks-controls-' . self::$controls_version);
|
||||
|
||||
if (!empty($reg_args['enqueue_script'])) {
|
||||
$script_handle = 'block-script-' . md5($reg_args['enqueue_script']);
|
||||
wp_register_script($script_handle, $reg_args['enqueue_script'], array(), $reg_args[ 'version' ], true);
|
||||
}
|
||||
|
||||
if (!empty($reg_args['enqueue_view_script'])) {
|
||||
$view_script_handle = 'block-script-' . md5($reg_args['enqueue_view_script']);
|
||||
wp_register_script($view_script_handle, $reg_args['enqueue_view_script'], array(), $reg_args[ 'version' ], true);
|
||||
}
|
||||
|
||||
if (!empty($reg_args['enqueue_editor_script'])) {
|
||||
$editor_custom_script_handle = 'block-script-' . md5($reg_args['enqueue_editor_script']);
|
||||
wp_register_script($editor_custom_script_handle, $reg_args['enqueue_editor_script'], $script_dependencies, $reg_args[ 'version' ]);
|
||||
$script_dependencies = array($editor_custom_script_handle);
|
||||
}
|
||||
|
||||
wp_register_script($editor_script_handle,
|
||||
$base_url . $reg_args[ 'js_file' ], $script_dependencies, $reg_args[ 'version' ]);
|
||||
|
||||
wp_localize_script($editor_script_handle,
|
||||
'pg_project_data_' . str_replace('-', '_', $slug),
|
||||
array(
|
||||
'url' => $base_url,
|
||||
)
|
||||
);
|
||||
|
||||
register_block_type($reg_args['name'], array(
|
||||
'render_callback' => empty($reg_args['dynamic']) ? null : function ($attributes, $content, $block) use ($reg_args) {
|
||||
self::$recursive_level++;
|
||||
if (self::$recursive_level > 10) {
|
||||
self::$recursive_level--;
|
||||
return 'Too many nested blocks... Are you including a post within itself?';
|
||||
}
|
||||
ob_start();
|
||||
$args = array('attributes' => $attributes, 'content' => $content, 'block' => $block);
|
||||
$template = trailingslashit($reg_args[ 'base_path' ]) . $reg_args['render_template'];
|
||||
if(file_exists( $template )) {
|
||||
require $template;
|
||||
} else {
|
||||
echo "<div>Dynamic block template $template not found.</div>";
|
||||
}
|
||||
self::$recursive_level--;
|
||||
return ob_get_clean();
|
||||
},
|
||||
'editor_script' => $editor_script_handle,
|
||||
'editor_style' => $editor_style_handle,
|
||||
'style' => $style_handle,
|
||||
'script' => $script_handle,
|
||||
'view_script' => $view_script_handle,
|
||||
'attributes' => $reg_args['attributes'],
|
||||
'supports' => isset($reg_args['supports']) ? $reg_args['supports'] : array()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
static function getDefault($args, $prop, $subprop = null)
|
||||
{
|
||||
if (isset($args['block']->block_type->attributes[$prop]['default'])) {
|
||||
if ($subprop) {
|
||||
if (isset($args['block']->block_type->attributes[$prop]['default'][$subprop])) {
|
||||
return $args['block']->block_type->attributes[$prop]['default'][$subprop];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return $args['block']->block_type->attributes[$prop]['default'];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static function getAttribute($args, $prop, $use_default = true, $null = false)
|
||||
{
|
||||
$val = isset($args['attributes']) && isset($args['attributes'][$prop]) ? $args['attributes'][$prop] : null;
|
||||
if (($val === null || $val === '') && $use_default) {
|
||||
$val = self::getDefault($args, $prop);
|
||||
}
|
||||
if($val === '' && $null) {
|
||||
$val = null;
|
||||
}
|
||||
return $val;
|
||||
}
|
||||
|
||||
static function getAttributeForAction($args, $prop, $field = null)
|
||||
{
|
||||
$val = self::getAttribute($args, $prop);
|
||||
if($field) {
|
||||
if(is_array($val) && isset($val[$field])) {
|
||||
return $val[$field];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if(is_array($val)) {
|
||||
if(isset($val['id'])) return $val['id'];
|
||||
}
|
||||
return $val;
|
||||
}
|
||||
|
||||
static function getAttributeAsPostIds($args, $prop)
|
||||
{
|
||||
$r = array();
|
||||
$list = self::getAttribute($args, $prop);
|
||||
if (is_array($list)) {
|
||||
foreach ($list as $item) {
|
||||
$r[] = $item['id'];
|
||||
}
|
||||
}
|
||||
if (count($r) === 0) $r[] = 0;
|
||||
return $r;
|
||||
}
|
||||
|
||||
static function getInnerContent($args)
|
||||
{
|
||||
return isset($args['content']) ? $args['content'] : '';
|
||||
}
|
||||
|
||||
static function getImageUrl($args, $prop, $size, $use_default = true)
|
||||
{
|
||||
$a = $args['attributes'];
|
||||
if (!isset($a[$prop])) return $use_default ? self::getDefault($args, $prop, 'url') : null;
|
||||
if (!empty($a[$prop]['url'])) {
|
||||
return $a[$prop]['url'];
|
||||
}
|
||||
if (empty($a[$prop]['id'])) return $use_default ? self::getDefault($args, $prop, 'url') : null;
|
||||
if (!empty($a[$prop]['size'])) {
|
||||
$size = $a[$prop]['size'];
|
||||
}
|
||||
return PG_Image::getUrl($a[$prop]['id'], $size);
|
||||
}
|
||||
|
||||
static function getImageSVG($args, $prop, $use_default = true)
|
||||
{
|
||||
return self::getImageField($args, $prop, 'svg', $use_default);
|
||||
}
|
||||
|
||||
static function getImageField($args, $prop, $field, $use_default = true)
|
||||
{
|
||||
$a = $args['attributes'];
|
||||
if (!isset($a[$prop])) return $use_default ? self::getDefault($args, $prop, $field) : null;
|
||||
if (!empty($a[$prop][$field])) {
|
||||
return $a[$prop][$field];
|
||||
}
|
||||
return $use_default ? self::getDefault($args, $prop, $field) : null;
|
||||
}
|
||||
|
||||
static function getLinkUrl($args, $prop, $use_default = true)
|
||||
{
|
||||
$a = self::getAttribute($args, $prop);
|
||||
if (is_array($a) && isset($a['url'])) {
|
||||
$val = $a['url'];
|
||||
} else {
|
||||
$val = $a;
|
||||
}
|
||||
if ($val === null || $val === '') $val = $use_default ? self::getDefault($args, $prop, 'url') : $val;
|
||||
return $val;
|
||||
}
|
||||
|
||||
static function mergeInlineSVGAttributes($svg, $props) {
|
||||
foreach($props as $prop => $val) {
|
||||
if($prop === 'className') $prop = 'class';
|
||||
if(is_array($val)) {
|
||||
$r = '';
|
||||
foreach($val as $key => $v) {
|
||||
$key = preg_replace_callback("/([A-Z])/g", function($m) {
|
||||
return '-'.strtolower($m[1]);
|
||||
}, $key);
|
||||
$r .= "$key:$v;";
|
||||
}
|
||||
$val = $r;
|
||||
}
|
||||
$q = '"';
|
||||
if(strpos($val, '"') >= 0) {
|
||||
$val = str_replace('"', """, $val);
|
||||
}
|
||||
$re = "/(<svg[^>]*\\s*)($prop=\"[^\"]*\")/i";
|
||||
if(preg_match($re, $svg)) {
|
||||
$svg = preg_replace($re, '$1' . $prop . '='.$q . $val . $q, $svg);
|
||||
} else {
|
||||
$svg = str_replace('<svg', "<svg $prop={$q}{$val}{$q}", $svg);
|
||||
}
|
||||
}
|
||||
return $svg;
|
||||
}
|
||||
|
||||
static function setupEditedPost() {
|
||||
global $wp_query, $post;
|
||||
if(!empty($_GET['context']) && $_GET['context'] === 'edit') {
|
||||
$referer = wp_get_raw_referer();
|
||||
if (!empty($referer)) {
|
||||
$url_info = parse_url($referer);
|
||||
$query = null;
|
||||
parse_str($url_info['query'], $query);
|
||||
if(!empty($query['post'])) {
|
||||
$post = get_post($query['post']);
|
||||
if(!empty($post)) {
|
||||
$wp_query->setup_postdata( $post );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
314
inc/wp_pg_helpers.php
Normal file
314
inc/wp_pg_helpers.php
Normal file
@@ -0,0 +1,314 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Name: PG_Image, PG_Helper...
|
||||
* GitHub URI:
|
||||
* Description:
|
||||
* Version: 1.0
|
||||
* Author: Matjaz Trontelj - @pinegrow
|
||||
* License: GPL-2.0+
|
||||
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
|
||||
*
|
||||
*/
|
||||
|
||||
if(! class_exists( 'PG_Image' )) {
|
||||
class PG_Image
|
||||
{
|
||||
|
||||
static function removeSizeAttributes($img, $attr = null)
|
||||
{
|
||||
if ($attr == 'both' || $attr == 'width') {
|
||||
$img = preg_replace('/\swidth="[^"]*"/i', '', $img);
|
||||
}
|
||||
if ($attr == 'both' || $attr == 'height') {
|
||||
$img = preg_replace('/\sheight="[^"]*"/i', '', $img);
|
||||
}
|
||||
return $img;
|
||||
}
|
||||
|
||||
static function getUrl($image_or_url, $size)
|
||||
{
|
||||
if (is_array($image_or_url)) {
|
||||
if (!empty($image_or_url['sizes']) && !empty($image_or_url['sizes'][$size])) {
|
||||
return $image_or_url['sizes'][$size];
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
if (!is_numeric($image_or_url)) {
|
||||
return $image_or_url;
|
||||
}
|
||||
return wp_attachment_is_image($image_or_url) ? wp_get_attachment_image_url($image_or_url, $size) : wp_get_attachment_url($image_or_url);
|
||||
}
|
||||
|
||||
static function getImages($list)
|
||||
{
|
||||
if (empty($list)) return array();
|
||||
if (is_string($list)) $list = explode(',', $list);
|
||||
$args = array(
|
||||
'post__in' => $list,
|
||||
'post_type' => 'attachment',
|
||||
'post_mime_type' => 'image',
|
||||
'posts_per_page' => -1,
|
||||
'orderby' => 'post__in',
|
||||
'order' => 'ASC'
|
||||
);
|
||||
return get_posts($args);
|
||||
}
|
||||
|
||||
static function isPostImage()
|
||||
{
|
||||
global $post;
|
||||
return $post->post_type === 'attachment' && !empty($post->post_mime_type) && strpos($post->post_mime_type, 'image') === 0;
|
||||
}
|
||||
|
||||
static function getPostImage($id, $size, $args, $remove_sizes = null, $default_img = null)
|
||||
{
|
||||
$img = '';
|
||||
|
||||
if (empty($id) && self::isPostImage()) {
|
||||
$img = wp_get_attachment_image(get_the_ID(), $size, false, $args);
|
||||
|
||||
} else if (has_post_thumbnail($id)) {
|
||||
$img = get_the_post_thumbnail($id, $size, $args);
|
||||
}
|
||||
|
||||
if (!empty($default_image) && empty($img)) {
|
||||
$img = $default_img;
|
||||
}
|
||||
|
||||
if (!empty($img) && !empty($remove_sizes)) {
|
||||
$img = self::removeSizeAttributes($img, $remove_sizes);
|
||||
}
|
||||
return $img;
|
||||
}
|
||||
|
||||
static function getAltText( $id, $default = '') {
|
||||
$r = trim( wp_strip_all_tags( get_post_meta( $id, '_wp_attachment_image_alt', true ) ) );
|
||||
return empty($r) ? $default : $r;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(! class_exists( 'PG_Helper_v2' )) {
|
||||
class PG_Helper_v2
|
||||
{
|
||||
|
||||
static function getPostFromSlug($slug_or_id, $post_type)
|
||||
{
|
||||
if (is_numeric($slug_or_id)) {
|
||||
return $slug_or_id;
|
||||
}
|
||||
return get_page_by_path($slug_or_id, OBJECT, $post_type);
|
||||
}
|
||||
|
||||
static function getTermFromSlug($slug_or_id, $taxonomy)
|
||||
{
|
||||
if (is_numeric($slug_or_id)) {
|
||||
return $slug_or_id;
|
||||
}
|
||||
switch ($taxonomy) {
|
||||
case 'category':
|
||||
return get_category_by_slug($slug_or_id);
|
||||
default:
|
||||
return get_term_by('slug', $slug_or_id, $taxonomy);
|
||||
}
|
||||
}
|
||||
|
||||
static function getTermIDFromSlug($slug_or_id, $taxonomy, $def = -1)
|
||||
{
|
||||
if (is_numeric($slug_or_id)) {
|
||||
return $slug_or_id;
|
||||
}
|
||||
$term = self::getTermFromSlug($slug_or_id, $taxonomy);
|
||||
return $term ? $term->term_id : $def;
|
||||
}
|
||||
|
||||
static function addAttributesToElements($tag, $attrs, $html)
|
||||
{
|
||||
$attr_str = '';
|
||||
|
||||
foreach ($attrs as $name => $val) {
|
||||
$attr_str .= " {$name}";
|
||||
if (!is_null($val)) {
|
||||
$attr_str .= "=\"{$val}\"";
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($attr_str)) {
|
||||
$html = str_replace("<{$tag} ", "<{$tag}{$attr_str} ", $html);
|
||||
$html = str_replace("<{$tag}>", "<{$tag}{$attr_str}>", $html);
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
static $shown_posts = array();
|
||||
|
||||
static function rememberShownPost($p = null)
|
||||
{
|
||||
global $post;
|
||||
if (empty($p)) {
|
||||
$p = $post;
|
||||
}
|
||||
if (!empty($p) && !in_array($p->ID, self::$shown_posts)) {
|
||||
self::$shown_posts[] = $p->ID;
|
||||
}
|
||||
}
|
||||
|
||||
static function getShownPosts()
|
||||
{
|
||||
return self::$shown_posts;
|
||||
}
|
||||
|
||||
static function getInsightMetaFields()
|
||||
{
|
||||
$list = array();
|
||||
$meta = get_post_meta(get_the_ID());
|
||||
if ($meta) {
|
||||
foreach ($meta as $key => $values) {
|
||||
if (strpos($key, '_') !== 0) {
|
||||
$list[] = $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
echo '<!-- PG_FIELDS:' . implode(',', $list) . '-->';
|
||||
}
|
||||
|
||||
static function getRelationshipFieldValue($field)
|
||||
{
|
||||
if (function_exists('get_field')) {
|
||||
return get_field($field, false, false);
|
||||
} else {
|
||||
$value = get_post_meta(get_the_ID(), $field);
|
||||
if (empty($value)) {
|
||||
return null;
|
||||
}
|
||||
if (count($value) === 1) {
|
||||
if (strpos($value[0], 'a:') >= 0 && strpos($value[0], '{') >= 0) {
|
||||
return unserialize($value[0]);
|
||||
}
|
||||
if (is_string($value[0])) {
|
||||
return explode(',', $value[0]);
|
||||
}
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
static function getPostIdList($value)
|
||||
{
|
||||
if (empty($value)) {
|
||||
return array(-1);
|
||||
}
|
||||
if (is_string($value)) {
|
||||
$value = explode(',', $value);
|
||||
}
|
||||
if (is_numeric($value)) {
|
||||
$value = array($value);
|
||||
}
|
||||
if (is_array($value) && count($value) === 0) {
|
||||
$value = array(-1);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
static function getBreadcrumbs($type = 'parents', $add_home = false, $home_label = '')
|
||||
{
|
||||
global $post;
|
||||
|
||||
$r = array();
|
||||
|
||||
if ($type === 'parents') {
|
||||
$parents = get_post_ancestors($post->ID);
|
||||
foreach ($parents as $parent_id) {
|
||||
$p = get_post($parent_id);
|
||||
$r[] = array(
|
||||
'name' => get_the_title($p),
|
||||
'link' => get_permalink($p)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$category = get_the_category($post->ID);
|
||||
|
||||
if (!empty($category)) {
|
||||
$parents = get_ancestors($category[0]->term_id, 'category');
|
||||
|
||||
array_unshift($parents, $category[0]->term_id);
|
||||
|
||||
foreach ($parents as $parent_id) {
|
||||
$p = get_category($parent_id);
|
||||
$r[] = array(
|
||||
'name' => $p->name,
|
||||
'link' => get_category_link($p)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( is_singular() ) {
|
||||
array_unshift($r, array(
|
||||
'name' => get_the_title($post),
|
||||
'link' => get_permalink($post)
|
||||
));
|
||||
}
|
||||
|
||||
if ($add_home) {
|
||||
$r[] = array(
|
||||
'name' => $home_label,
|
||||
'link' => home_url()
|
||||
);
|
||||
}
|
||||
|
||||
return array_reverse($r);
|
||||
}
|
||||
|
||||
static function getArray( $d, $separator = ',' ) {
|
||||
if(is_array( $d )) return $d;
|
||||
if(is_null( $d ) || $d === '') return array();
|
||||
if(is_string( $d )) {
|
||||
return array_map('trim', explode( $separator, $d));
|
||||
} else {
|
||||
return array( $d );
|
||||
}
|
||||
}
|
||||
|
||||
static function getTaxonomyQuery( $taxonomy, $terms ) {
|
||||
$is_or = true;
|
||||
if(is_string( $terms )) {
|
||||
if(strpos( $terms, '+') !== false) {
|
||||
$is_or = false;
|
||||
}
|
||||
}
|
||||
$terms = self::getArray( $terms, $is_or ? ',' : '+');
|
||||
if( count( $terms ) === 0 ) return null;
|
||||
|
||||
$field = 'term_id';
|
||||
for($i = 0; $i < count( $terms ); $i++) {
|
||||
if(!is_numeric( $terms[$i] )) {
|
||||
$field = 'slug';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'taxonomy' => $taxonomy,
|
||||
'field' => $field,
|
||||
'terms' => $terms,
|
||||
'include_children' => true,
|
||||
'operator' => $is_or ? 'IN' : 'AND'
|
||||
);
|
||||
}
|
||||
|
||||
static function getCurrentPost() {
|
||||
global $post;
|
||||
return $post;
|
||||
}
|
||||
}
|
||||
|
||||
//Compatibility with any existing custom code
|
||||
if(! class_exists( 'PG_Helper' )) {
|
||||
class PG_Helper extends PG_Helper_v2 {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
72
inc/wp_pg_pagination.php
Normal file
72
inc/wp_pg_pagination.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Name: PG_Pagination
|
||||
* GitHub URI:
|
||||
* Description:
|
||||
* Version: 1.0
|
||||
* Author: Matjaz Trontelj - @pinegrow
|
||||
* License: GPL-2.0+
|
||||
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
class PG_Pagination
|
||||
{
|
||||
static function getQuery( $query = null ) {
|
||||
global $wp_query;
|
||||
|
||||
if(empty( $query )) {
|
||||
return $wp_query;
|
||||
} else {
|
||||
return $query;
|
||||
}
|
||||
}
|
||||
|
||||
static function getCurrentPage() {
|
||||
$name = 'paged';
|
||||
if ( 'page' == get_option( 'show_on_front' ) && is_front_page() ) {
|
||||
$name = 'page';
|
||||
}
|
||||
return intval(get_query_var( $name ) ? get_query_var( $name ) : 1);
|
||||
}
|
||||
|
||||
static function getMaxPages( $query = null ) {
|
||||
return self::getQuery( $query )->max_num_pages;
|
||||
}
|
||||
|
||||
static function isPaginated( $query = null ) {
|
||||
return self::getQuery( $query )->max_num_pages > 1;
|
||||
}
|
||||
|
||||
static function getNextPageUrl( $query = null ) {
|
||||
$max_pages = self::getMaxPages( $query );
|
||||
if(self::getCurrentPage() < $max_pages) {
|
||||
return get_pagenum_link( self::getCurrentPage() + 1 );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static function getPreviousPageUrl( $query = null ) {
|
||||
if(self::getCurrentPage() > 1) {
|
||||
return get_pagenum_link( self::getCurrentPage() - 1 );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static function getHrefAttribute( $url ) {
|
||||
if(empty( $url )) {
|
||||
return 'href="javascript:void(0)"';
|
||||
} else {
|
||||
return 'href="'.esc_url( $url ).'"';
|
||||
}
|
||||
}
|
||||
|
||||
static function getPreviousHrefAttribute( $query = null ) {
|
||||
return self::getHrefAttribute( self::getPreviousPageUrl( $query ));
|
||||
}
|
||||
|
||||
static function getNextHrefAttribute( $query = null ) {
|
||||
return self::getHrefAttribute( self::getNextPageUrl( $query ));
|
||||
}
|
||||
}
|
239
inc/wp_simple_form_mailer.php
Normal file
239
inc/wp_simple_form_mailer.php
Normal file
@@ -0,0 +1,239 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Name: PG_Simple_Form_Mailer
|
||||
* GitHub URI:
|
||||
* Description:
|
||||
* Version: 1.0
|
||||
* Author: Matjaz Trontelj - @pinegrow
|
||||
* License: GPL-2.0+
|
||||
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
class PG_Simple_Form_Mailer {
|
||||
|
||||
static $captcha_script_included = false;
|
||||
|
||||
public $processed = false;
|
||||
public $error = true;
|
||||
public $message = 'The form was not submitted';
|
||||
public $text = null;
|
||||
public $html = null;
|
||||
|
||||
public function process( $arg_options = array() ) {
|
||||
|
||||
$admin_email = get_option('admin_email');
|
||||
|
||||
$options = array(
|
||||
'form_id' => 'contact_form',
|
||||
'send_to_email' => false,
|
||||
'email' => $admin_email,
|
||||
'title' => 'Contact form submission',
|
||||
'intro' => 'We received a new contact form submission:',
|
||||
'save_to_post_type' => null,
|
||||
'post_type' => null,
|
||||
'captcha' => false,
|
||||
'captcha_key' => null,
|
||||
'captcha_secret' => null,
|
||||
'include_captcha_script' => false,
|
||||
'log_ip' => true,
|
||||
'success_message' => 'Thank you for getting in touch!',
|
||||
'error_message' => 'There was a problem submitting this form. Please contact us directly.',
|
||||
'callback' => null
|
||||
);
|
||||
|
||||
//merge options
|
||||
foreach($arg_options as $key => $value) {
|
||||
$options[ $key ] = $value;
|
||||
}
|
||||
|
||||
if($options['captcha']) {
|
||||
if (!empty($options['include_captcha_script']) && !PG_Simple_Form_Mailer::$captcha_script_included) {
|
||||
echo '<script src="https://www.google.com/recaptcha/api.js" async defer></script>';
|
||||
PG_Simple_Form_Mailer::$captcha_script_included = true;
|
||||
}
|
||||
}
|
||||
|
||||
if( !empty($_POST[$options['form_id']]) ) {
|
||||
//the form was submitted
|
||||
//we assume the browser did the validation
|
||||
$lf = "\n\r";
|
||||
|
||||
$ignore_fields = array($options['form_id'], 'g-recaptcha-response');
|
||||
|
||||
$text = $options['intro'].$lf.$lf;
|
||||
$html = "<p>{$options['intro']}</p>";
|
||||
|
||||
$from_email = null;
|
||||
|
||||
$this->processed = true;
|
||||
|
||||
if($options['captcha']) {
|
||||
|
||||
if(empty($options['captcha_key']) || empty($options['captcha_secret'])) {
|
||||
$this->error = 'Captcha key and secret are not set.';
|
||||
return true;
|
||||
}
|
||||
|
||||
if(empty($_POST['g-recaptcha-response'])) {
|
||||
$this->error = 'Captcha response is not present.';
|
||||
return true;
|
||||
|
||||
} else if($this->validate_rechapcha($_POST['g-recaptcha-response'], $options['captcha_secret']) !== true) {
|
||||
$this->error = 'Captcha validation failed.';
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
$form_data = array();
|
||||
|
||||
foreach($_POST as $key => $value) {
|
||||
if(!in_array( $key, $ignore_fields)) {
|
||||
$key = htmlspecialchars($key);
|
||||
$value = htmlspecialchars($value);
|
||||
|
||||
$text .= "{$key}: {$value}".$lf;
|
||||
|
||||
if($key == 'email' || $key == 'Email') {
|
||||
$from_email = $value;
|
||||
}
|
||||
|
||||
$html .= "<p><b>{$key}</b>: {$value}</p>";
|
||||
|
||||
$form_data[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$stamp = "Submitted on ".date("F j, Y, g:i a");
|
||||
if($options['log_ip'] && !empty($_SERVER['REMOTE_ADDR'])) {
|
||||
$stamp .= " from ".$_SERVER['REMOTE_ADDR'];
|
||||
}
|
||||
|
||||
$text .= $stamp;
|
||||
$html .= "<p><em>{$stamp}</em></p>";
|
||||
|
||||
$this->text = $text;
|
||||
$this->html = $html;
|
||||
|
||||
$emailed = null;
|
||||
$saved = null;
|
||||
$called = null;
|
||||
|
||||
if($options['send_to_email']) {
|
||||
$headers = 'From: '. $admin_email . "\r\n";
|
||||
|
||||
$email_data = array(
|
||||
'headers' => $headers,
|
||||
'subject' => $options['title'],
|
||||
'content' => $text,
|
||||
'data' => $form_data
|
||||
);
|
||||
|
||||
$email_data = apply_filters( 'pg_form_email', $email_data);
|
||||
|
||||
$emailed = wp_mail($options['email'], $email_data['subject'], $email_data['content'], $email_data['headers']);
|
||||
}
|
||||
if($options['save_to_post_type']) {
|
||||
if(wp_insert_post( array(
|
||||
'post_title' => $options['title'].(!empty( $from_email ) ? (" - ".$from_email) : ""),
|
||||
'post_content' => $html,
|
||||
'post_type' => $options['post_type'],
|
||||
'post_status' => 'private'
|
||||
) )) {
|
||||
$saved = true;
|
||||
} else {
|
||||
$saved = false;
|
||||
}
|
||||
}
|
||||
|
||||
if($options['callback']) {
|
||||
if(function_exists($options['callback'])) {
|
||||
$called = call_user_func($options['callback'], $form_data, $options);
|
||||
} else {
|
||||
$called = false;
|
||||
}
|
||||
}
|
||||
|
||||
if((!$emailed && !$saved && !$called) || $emailed === false || $saved === false || $called === false) {
|
||||
$this->error = true;
|
||||
$this->message = $options['error_message'];
|
||||
} else {
|
||||
$this->error = false;
|
||||
$this->message = $options['success_message'];
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
//the form was not submitted
|
||||
$this->processed = false;
|
||||
$this->error = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//source https://gist.github.com/jonathanstark/dfb30bdfb522318fc819
|
||||
public function validate_rechapcha($response, $secret)
|
||||
{
|
||||
// Verifying the user's response (https://developers.google.com/recaptcha/docs/verify)
|
||||
$verifyURL = 'https://www.google.com/recaptcha/api/siteverify';
|
||||
|
||||
$query_data = [
|
||||
'secret' => $secret,
|
||||
'response' => $response,
|
||||
'remoteip' => (isset($_SERVER["HTTP_CF_CONNECTING_IP"]) ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER['REMOTE_ADDR'])
|
||||
];
|
||||
|
||||
// Collect and build POST data
|
||||
$post_data = http_build_query($query_data, '', '&');
|
||||
|
||||
// Send data on the best possible way
|
||||
if (function_exists('curl_init') && function_exists('curl_setopt') && function_exists('curl_exec'))
|
||||
{
|
||||
// Use cURL to get data 10x faster than using file_get_contents or other methods
|
||||
$ch = curl_init($verifyURL);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
||||
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-type: application/x-www-form-urlencoded'));
|
||||
$response = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If server not have active cURL module, use file_get_contents
|
||||
$opts = array('http' =>
|
||||
array(
|
||||
'method' => 'POST',
|
||||
'header' => 'Content-type: application/x-www-form-urlencoded',
|
||||
'content' => $post_data
|
||||
)
|
||||
);
|
||||
$context = stream_context_create($opts);
|
||||
$response = file_get_contents($verifyURL, false, $context);
|
||||
}
|
||||
|
||||
// Verify all reponses and avoid PHP errors
|
||||
if ($response)
|
||||
{
|
||||
$result = json_decode($response);
|
||||
if ($result->success === true)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
// Dead end
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
279
inc/wp_smart_navwalker.php
Normal file
279
inc/wp_smart_navwalker.php
Normal file
@@ -0,0 +1,279 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Name: PG_Smart_Walker_Nav_Menu
|
||||
* GitHub URI:
|
||||
* Description:
|
||||
* Version: 2.0
|
||||
* Author: Matjaz Trontelj - @pinegrow
|
||||
* License: GPL-2.0+
|
||||
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
|
||||
* Based on WordPress Walker_Nav_Menu code
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
class PG_Smart_Walker_Nav_Menu extends Walker_Nav_Menu {
|
||||
|
||||
private static $default_options = array(
|
||||
'top_element' => 'ul',
|
||||
'sub_element' => 'ul',
|
||||
'current_class' => 'current-menu-item',
|
||||
'sub_menu_class' => 'sub-menu',
|
||||
'item_id_prefix' => 'menu-item-',
|
||||
'template' => '<li id="{ID}" class="{CLASSES}">{LINK_BEFORE}<a {ATTRS}>{TITLE}</a>{LINK_AFTER}</li>',
|
||||
'template_item_with_sublevel' => null,
|
||||
'template_sublevel' => null,
|
||||
'template_subitem' => null
|
||||
);
|
||||
|
||||
public static $options = array();
|
||||
|
||||
private static $level = 0;
|
||||
private static $current_id = '';
|
||||
private static $count = 0;
|
||||
|
||||
public static function init() {
|
||||
self::$options = self::$default_options;
|
||||
self::$level = 0;
|
||||
self::$current_id = '';
|
||||
self::$count++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the begining or end part of the template
|
||||
*
|
||||
* @param boolean $start true for begining, false for end part
|
||||
*/
|
||||
private function get_template_part( $start = true, $template = 'template' ) {
|
||||
$parts = explode('{SUB}', self::$options[ $template ]);
|
||||
if(count($parts) == 2) {
|
||||
return $start ? $parts[0] : $parts[1];
|
||||
}
|
||||
//if {SUB} is missing from the template, assume the last closing tag is the end part
|
||||
$idx = strrpos(self::$options[ $template ], '</');
|
||||
if($idx !== false) {
|
||||
return $start ? substr(self::$options[ $template ], 0, $idx) : substr(self::$options[ $template ], $idx);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the list before the elements are added.
|
||||
*
|
||||
* @see Walker::start_lvl()
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @param string $output Passed by reference. Used to append additional content.
|
||||
* @param int $depth Depth of menu item. Used for padding.
|
||||
* @param array $args An array of arguments. @see wp_nav_menu()
|
||||
*/
|
||||
public function start_lvl( &$output, $depth = 0, $args = array() ) {
|
||||
self::$level++;
|
||||
|
||||
$indent = str_repeat("\t", $depth);
|
||||
|
||||
if(self::$level > 0 && self::$options['template_sublevel']) {
|
||||
$output .= "\n$indent" . str_replace('{ID}', self::$current_id,self::get_template_part(true, 'template_sublevel'));
|
||||
} else {
|
||||
$tag = $depth == 0 ? self::$options['top_element'] : self::$options['sub_element'];
|
||||
if (!empty($tag)) {
|
||||
$class = self::$options['sub_menu_class'];
|
||||
$output .= "\n$indent<$tag class=\"$class\">\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends the list of after the elements are added.
|
||||
*
|
||||
* @see Walker::end_lvl()
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @param string $output Passed by reference. Used to append additional content.
|
||||
* @param int $depth Depth of menu item. Used for padding.
|
||||
* @param array $args An array of arguments. @see wp_nav_menu()
|
||||
*/
|
||||
public function end_lvl( &$output, $depth = 0, $args = array() ) {
|
||||
$indent = str_repeat("\t", $depth);
|
||||
|
||||
if(self::$level > 0 && self::$options['template_sublevel']) {
|
||||
$output .= "\n$indent" . self::get_template_part(false, 'template_sublevel');
|
||||
} else {
|
||||
$tag = $depth == 0 ? self::$options['top_element'] : self::$options['sub_element'];
|
||||
if (!empty($tag)) {
|
||||
$output .= "$indent</$tag>\n";
|
||||
}
|
||||
}
|
||||
self::$level--;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the element output.
|
||||
*
|
||||
* @see Walker::start_el()
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @param string $output Passed by reference. Used to append additional content.
|
||||
* @param object $item Menu item data object.
|
||||
* @param int $depth Depth of menu item. Used for padding.
|
||||
* @param array $args An array of arguments. @see wp_nav_menu()
|
||||
* @param int $id Current item ID.
|
||||
*/
|
||||
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
|
||||
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
|
||||
|
||||
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
|
||||
$classes[] = 'menu-item-' . $item->ID;
|
||||
|
||||
/**
|
||||
* Filter the CSS class(es) applied to a menu item's list item element.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 4.1.0 The `$depth` parameter was added.
|
||||
*
|
||||
* @param array $classes The CSS classes that are applied to the menu item's `<li>` element.
|
||||
* @param object $item The current menu item.
|
||||
* @param array $args An array of {@see wp_nav_menu()} arguments.
|
||||
* @param int $depth Depth of menu item. Used for padding.
|
||||
*/
|
||||
$classes = apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth );
|
||||
|
||||
foreach($classes as $i => $class) {
|
||||
if($classes[$i] == 'current-menu-item') {
|
||||
$classes[$i] = self::$options['current_class'];
|
||||
}
|
||||
}
|
||||
$class_names = join( ' ', $classes);
|
||||
|
||||
/**
|
||||
* Filter the ID applied to a menu item's list item element.
|
||||
*
|
||||
* @since 3.0.1
|
||||
* @since 4.1.0 The `$depth` parameter was added.
|
||||
*
|
||||
* @param string $menu_id The ID that is applied to the menu item's `<li>` element.
|
||||
* @param object $item The current menu item.
|
||||
* @param array $args An array of {@see wp_nav_menu()} arguments.
|
||||
* @param int $depth Depth of menu item. Used for padding.
|
||||
*/
|
||||
$id = apply_filters( 'nav_menu_item_id', self::$options['item_id_prefix']. $item->ID, $item, $args, $depth );
|
||||
|
||||
if(empty($id)) {
|
||||
//most likely the default id was already used. Set a count based id
|
||||
$id = self::$options['item_id_prefix'].self::$count.'-'.$item->ID;
|
||||
}
|
||||
|
||||
self::$current_id = $id;
|
||||
|
||||
$output .= $indent;
|
||||
|
||||
$atts = array();
|
||||
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
|
||||
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
|
||||
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
|
||||
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
|
||||
|
||||
/**
|
||||
* Filter the HTML attributes applied to a menu item's anchor element.
|
||||
*
|
||||
* @since 3.6.0
|
||||
* @since 4.1.0 The `$depth` parameter was added.
|
||||
*
|
||||
* @param array $atts {
|
||||
* The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
|
||||
*
|
||||
* @type string $title Title attribute.
|
||||
* @type string $target Target attribute.
|
||||
* @type string $rel The rel attribute.
|
||||
* @type string $href The href attribute.
|
||||
* }
|
||||
* @param object $item The current menu item.
|
||||
* @param array $args An array of {@see wp_nav_menu()} arguments.
|
||||
* @param int $depth Depth of menu item. Used for padding.
|
||||
*/
|
||||
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
|
||||
|
||||
$attributes = '';
|
||||
foreach ( $atts as $attr => $value ) {
|
||||
if ( ! empty( $value ) ) {
|
||||
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
|
||||
$attributes .= ' ' . $attr . '="' . $value . '"';
|
||||
}
|
||||
}
|
||||
|
||||
$item_output = $args->before;
|
||||
|
||||
$has_sublevel = false;
|
||||
|
||||
if(isset($item->classes) && in_array('menu-item-has-children', $item->classes)) {
|
||||
$has_sublevel = true;
|
||||
}
|
||||
|
||||
$template_key = 'template';
|
||||
if(self::$level === 0 && $has_sublevel && self::$options['template_item_with_sublevel']) {
|
||||
$template_key = 'template_item_with_sublevel';
|
||||
} else if(self::$level > 0 && self::$options['template_subitem']) {
|
||||
$template_key = 'template_subitem';
|
||||
}
|
||||
|
||||
$template = $this->get_template_part(true, $template_key);
|
||||
$template = str_replace( '{LINK_BEFORE}', $args->link_before, $template);
|
||||
$template = str_replace( '{LINK_AFTER}', $args->link_after, $template);
|
||||
$template = str_replace( '{TITLE}', apply_filters( 'the_title', $item->title, $item->ID ), $template);
|
||||
|
||||
$template = str_replace( '{ATTRS}', $attributes, $template);
|
||||
$template = str_replace( '{ID}', $id, $template);
|
||||
$template = str_replace( '{CLASSES}', $class_names, $template);
|
||||
|
||||
$item_output .= $template;
|
||||
|
||||
$item_output .= $args->after;
|
||||
|
||||
/**
|
||||
* Filter a menu item's starting output.
|
||||
*
|
||||
* The menu item's starting output only includes `$args->before`, the opening `<a>`,
|
||||
* the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
|
||||
* no filter for modifying the opening and closing `<li>` for a menu item.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @param string $item_output The menu item's starting HTML output.
|
||||
* @param object $item Menu item data object.
|
||||
* @param int $depth Depth of menu item. Used for padding.
|
||||
* @param array $args An array of {@see wp_nav_menu()} arguments.
|
||||
*/
|
||||
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends the element output, if needed.
|
||||
*
|
||||
* @see Walker::end_el()
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @param string $output Passed by reference. Used to append additional content.
|
||||
* @param object $item Page data object. Not used.
|
||||
* @param int $depth Depth of page. Not Used.
|
||||
* @param array $args An array of arguments. @see wp_nav_menu()
|
||||
*/
|
||||
public function end_el( &$output, $item, $depth = 0, $args = array() ) {
|
||||
$has_sublevel = false;
|
||||
|
||||
if(isset($item->classes) && in_array('menu-item-has-children', $item->classes)) {
|
||||
$has_sublevel = true;
|
||||
}
|
||||
|
||||
$template_key = 'template';
|
||||
if(self::$level === 0 && $has_sublevel && self::$options['template_item_with_sublevel']) {
|
||||
$template_key = 'template_item_with_sublevel';
|
||||
} else if(self::$level > 0 && self::$options['template_subitem']) {
|
||||
$template_key = 'template_subitem';
|
||||
}
|
||||
$output .= $this->get_template_part(false, $template_key)."\n"; //get the ending part of the template
|
||||
}
|
||||
|
||||
} // PG_Smart_Walker_Nav_Menu
|
Reference in New Issue
Block a user