_x(ucwords($plural_name), 'post type general name', ALCHEMYST_FORMS_TEXTDOMAIN),
'singular_name' => _x(ucwords($singular_name), 'post type singular name', ALCHEMYST_FORMS_TEXTDOMAIN),
'menu_name' => _x(ucwords($plural_name), 'admin menu', ALCHEMYST_FORMS_TEXTDOMAIN),
'name_admin_bar' => _x(ucwords($singular_name), 'add new on admin bar', ALCHEMYST_FORMS_TEXTDOMAIN),
'add_new' => _x('Add New', strtolower($singular_name), ALCHEMYST_FORMS_TEXTDOMAIN),
'add_new_item' => __('Add New ' . ucwords($singular_name), ALCHEMYST_FORMS_TEXTDOMAIN),
'new_item' => __('New ' . ucwords($singular_name), ALCHEMYST_FORMS_TEXTDOMAIN),
'edit_item' => __('Edit ' . ucwords($singular_name), ALCHEMYST_FORMS_TEXTDOMAIN),
'view_item' => __('View ' . ucwords($singular_name), ALCHEMYST_FORMS_TEXTDOMAIN),
'all_items' => __('All ' . ucwords($plural_name), ALCHEMYST_FORMS_TEXTDOMAIN),
'search_items' => __('Search ' . ucwords($plural_name), ALCHEMYST_FORMS_TEXTDOMAIN),
'parent_item_colon' => __('Parent ' . ucwords($plural_name) . ':', ALCHEMYST_FORMS_TEXTDOMAIN),
'not_found' => __('No ' . strtolower($plural_name) . ' found.', ALCHEMYST_FORMS_TEXTDOMAIN),
'not_found_in_trash' => __('No ' . strtolower($plural_name) . ' found in Trash.', ALCHEMYST_FORMS_TEXTDOMAIN)
);
$defaults = array(
'labels' => $labels,
'_builtin' => false,
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'hierarchical' => false,
'capability_type' => 'post',
'rewrite' => array(
'slug' => strtolower($post_type_name),
'with_front' => true,
),
'supports' => array(
'title',
'editor',
'thumbnail',
'excerpt',
'custom-fields',
'comments'
)
);
$args = array_merge($defaults, $sender_args);
register_post_type($post_type_name, $args);
};
add_action('init', $lambda);
}
/**
* Easier way to regiser taxonomies witout having to worry so much about labels. Any args can be overwritten with $sender_args
* $supported_post_types can be a string or an array
*
* @param $taxonomy_name - Name of this taxonomy. Should be slug-like.
* @param $supported_post_types - Single string, or array of supported post types.
* @param $singular_name - Singular name - capitalization does not matter.
* @param $plural_name - Plural name, capitalization does not matter.
* @param $sender_args - Override any of the default args that this function assumes, or add additional ones.
*/
public static function register_taxonomy($taxonomy_name, $supported_post_types, $singular_name, $plural_name, $sender_args = array()) {
$lambda = function() use (&$taxonomy_name, &$supported_post_types, &$singular_name, &$plural_name, &$sender_args) {
$labels = array(
'name' => _x(ucwords($plural_name), 'taxonomy general name', ALCHEMYST_FORMS_TEXTDOMAIN),
'singular_name' => _x(ucwords($singular_name), 'taxonomy singular name', ALCHEMYST_FORMS_TEXTDOMAIN),
'search_items' => __('Search ' . ucwords($plural_name), ALCHEMYST_FORMS_TEXTDOMAIN),
'all_items' => __('All ' . ucwords($plural_name), ALCHEMYST_FORMS_TEXTDOMAIN),
'parent_item' => __('Parent ' . ucwords($singular_name), ALCHEMYST_FORMS_TEXTDOMAIN),
'parent_item_colon' => __('Parent ' . ucwords($singular_name) . ':', ALCHEMYST_FORMS_TEXTDOMAIN),
'edit_item' => __('Edit ' . ucwords($singular_name), ALCHEMYST_FORMS_TEXTDOMAIN),
'update_item' => __('Update ' . ucwords($singular_name), ALCHEMYST_FORMS_TEXTDOMAIN),
'add_new_item' => __('Add New ' . ucwords($singular_name), ALCHEMYST_FORMS_TEXTDOMAIN),
'new_item_name' => __('New ' . ucwords($singular_name) . ' Name', ALCHEMYST_FORMS_TEXTDOMAIN),
'menu_name' => __(ucwords($plural_name), ALCHEMYST_FORMS_TEXTDOMAIN),
);
$defaults = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => strtolower($singular_name)),
);
$args = array_merge($defaults, $sender_args);
register_taxonomy($taxonomy_name, $supported_post_types, $args);
};
add_action('init', $lambda);
}
/**
* Nonce Helpers
* These are necessary because the Wordpress implementation of nonce is totally not what a nonce actually should be.
* Nonce should be one time use only, and always.
*
* NOTE: These do not work properly yet (but they do work equally well to how the wp nonces work)
* TODO: Write custom implementation of nonce?
*
* @param $nonce_name - Name of this Nonce. Will be additionally sanitized with a microtime(true) call.
*/
public static function create_nonce($nonce_name) {
$mt = microtime(true);
$nonce_val = wp_create_nonce($nonce_name . $mt);
$nonce_str = $mt . '-' . $nonce_val;
return $nonce_str;
}
/**
* Verify a nonce built with Alchemyst_Forms_Utils::create_nonce
*
* @param $nonce - Nonce to verify;
* @param $nonce_name - Name of the nonce to verify - sans the microtime of course.
*/
public static function verify_nonce($nonce, $nonce_name) {
$nonce_parts = explode('-', $nonce);
// parts[0] is the creation microtime, parts[1] is the nonce_val to verify
if (!is_array($nonce_parts)) return 0;
if (count($nonce_parts) != 2) return -1;
return wp_verify_nonce($nonce_parts[1], $nonce_name . $nonce_parts[0]);
}
/**
* Takes a nasty looking array and makes it look prettier.
* Do not use on get_post_meta where keys are not always unique.
*
* @param $meta - Expects the results from get_post_meta($post_id) (with no additional parameters)
*/
public static function clean_meta($meta) {
$meta_holder = array();
if (!is_array($meta)) {
return false;
}
foreach ($meta as $key => $val) {
$meta_holder[$key] = maybe_unserialize($val[0]);
}
return $meta_holder;
}
/**
* Simple array to string conversion.
* Supports multidimensional arrays (implode() does not)
*
* @param $array - Array to convert to string.
* @param $glue - Glue to piece the array together with. Will be trimmed from the end.
*/
public static function array_to_string($array, $glue = ', ') {
$ret = '';
foreach ($array as $item) {
if (is_array($item)) {
$ret .= self::array_to_string($item, $glue) . $glue;
} else {
$ret .= $item . $glue;
}
}
$ret = substr($ret, 0, 0-strlen($glue));
return $ret;
}
/**
* This used to be a custom implementation. Now it just wraps around Wordpress' sanitize_title function.
* Maintained for compatibility, and slugify makes more sense as a function name to me.
*
* @param $text - text to slugify.
*/
public static function slugify($text) {
return sanitize_title($text);
}
/**
* Simple format helper
* Will turn a string like 'this-is-a-field-name' to 'This Is A Field Name'
*
* @param $string - String to unslugify. Not guaranteed to be a match of how the string was before being slugified.
*/
public static function unslugify($string) {
return ucwords(str_replace("_", " ", str_replace('-', ' ', $string)));
}
/**
* Turn an integer into a more readable format (bytes to KB, MB, GB)
* Modified from @source http://stackoverflow.com/a/2510540
*
* @param $size - Number of bytes.
* @param $precision - Number of decimal places to include in the round().
*/
public static function format_bytes($size, $precision = 2) {
$base = log($size, 1024);
$suffixes = array('', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB');
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
}
// Holy function name, Batman!
public static function get_interpolation_options_as_string_from_message($message) {
$pattern = '/\[(\w+)\]/';
preg_match_all($pattern, $message, $matches);
return self::array_to_string(array_unique($matches[0]));
}
public static function get_encryption_key() {
$wud = wp_upload_dir();
$location = $wud['basedir'] . '/alchemyst-contact-forms/encryption-key.txt';
if (!file_exists($location)) {
self::write_encryption_key();
}
$key = file_get_contents($location);
return $key;
}
public static function write_encryption_key() {
$wud = wp_upload_dir();
$location = $wud['basedir'] . '/alchemyst-contact-forms/encryption-key.txt';
if (file_exists($location)) return;
if (!is_dir(dirname($location))) {
mkdir(dirname($location), 0755, true);
}
$f = fopen($location, 'w');
if ($f === false) {
self::_exit('We had some troubles writing an encryption key. Your server may be misconfigured');
}
fwrite($f, base64_encode(openssl_random_pseudo_bytes(4096)));
fclose($f);
// Write an htaccess to deny access to this file.
$location2 = $wud['basedir'] . '/alchemyst-contact-forms/.htaccess';
$f2 = fopen($location2, 'w');
if ($f2 === false) {
self::_exit('We had some troubles writing an encryption key. Your server may be misconfigured');
}
fwrite($f2, "