Last active
August 9, 2017 16:10
-
-
Save pierre-dargham/8c4f5ee60c48ffa4a265e75fe04c7bbb to your computer and use it in GitHub Desktop.
wp-cubi-helpers.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Globalis\WP\Cubi; | |
function get_permalink_by_template($template, $default = false) | |
{ | |
$pages = new \WP_Query([ | |
'posts_per_page' => 1, | |
'post_type' => 'page', | |
'post_status' => 'publish', | |
'meta_key' => '_wp_page_template', | |
'meta_value' => $template, | |
]); | |
if (empty($pages->posts)) { | |
return $default; | |
} | |
return get_permalink(reset($pages->posts)->ID); | |
} | |
function include_template_part($file, $data = [], $return = false) | |
{ | |
ob_start(); | |
extract($data); | |
include locate_template($file . '.php'); | |
if ($return) { | |
return ob_get_clean(); | |
} else { | |
ob_end_flush(); | |
} | |
} | |
function str_starts_with($string, $search) | |
{ | |
return substr($string, 0, strlen($search)) === $search; | |
} | |
function str_ends_with($string, $search) | |
{ | |
return substr($string, -strlen($search)) === $search; | |
} | |
function get_current_url($remove_query_args = false) | |
{ | |
$url = WP_SCHEME . '://' . WP_DOMAIN . $_SERVER['REQUEST_URI']; | |
if ($remove_query_args) { | |
$parts = parse_url($url); | |
$url = WP_SCHEME . '://' . WP_DOMAIN . $parts['path']; | |
} | |
return $url; | |
} | |
function current_url_starts_with($search) | |
{ | |
return str_starts_with(get_current_url(), $search); | |
} | |
function current_url_ends_with($search) | |
{ | |
return str_ends_with(get_current_url(), $search); | |
} | |
function remove_filter_anonymous_object($tag, $class_name, $method_name, $priority = 10) | |
{ | |
global $wp_filter; | |
if (!isset($wp_filter[$tag])) { | |
return false; | |
} | |
if (is_a($wp_filter[$tag], 'WP_Hook')) { | |
$callbacks = $wp_filter[$tag]->callbacks; // >= WordPress 4.7 | |
} else { | |
$callbacks = $wp_filter[$tag]; // < WordPress 4.7 | |
} | |
if (!isset($callbacks[$priority]) || !is_array($callbacks[$priority])) { | |
return false; | |
} | |
$removed = 0; | |
foreach ($callbacks[$priority] as $callback) { | |
if (is_array($callback) && isset($callback['function'][0]) && isset($callback['function'][1])) { | |
$object = $callback['function'][0]; | |
$method = $callback['function'][1]; | |
if (is_a($object, $class_name) && $method_name === $method) { | |
if (remove_filter($tag, [$object, $method_name], $priority)) { | |
$removed++; | |
} | |
} | |
} | |
} | |
return $removed > 0; | |
} | |
function is_ajax() | |
{ | |
return defined('DOING_AJAX') && DOING_AJAX; | |
} | |
function trigger_404() | |
{ | |
global $wp_query; | |
$wp_query->set_404(); | |
status_header(404); | |
} | |
function increase_server_limits($time_limit = 0, $memory_limit = '512M') | |
{ | |
ini_set('memory_limit', $memory_limit); | |
ini_set('max_execution_time', $time_limit); | |
set_time_limit($time_limit); | |
} | |
function wrap_mail_html($title, $content) | |
{ | |
ob_start(); | |
?> | |
<!DOCTYPE html> | |
<html <?php language_attributes(); ?>> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=<?php bloginfo('charset'); ?>" /> | |
<title><?php echo $title; ?></title> | |
</head> | |
<body <?php echo is_rtl() ? 'rightmargin' : 'leftmargin'; ?>="0" marginwidth="0" topmargin="0" marginheight="0" offset="0"> | |
<?= $content; ?> | |
</body> | |
</html> | |
<?php | |
return ob_get_clean(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment