first commit
This commit is contained in:
@@ -0,0 +1,221 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Admin Scripts and Styles
|
||||
*
|
||||
* Enqueue scripts and styles to be used on the admin pages
|
||||
*
|
||||
* @since 3.1.0
|
||||
*
|
||||
* @param string $hook Hook suffix for the current admin page
|
||||
*/
|
||||
function wpcf7dtx_enqueue_admin_assets($hook)
|
||||
{
|
||||
//Only load on CF7 Form pages
|
||||
if ($hook == 'toplevel_page_wpcf7') {
|
||||
$prefix = 'wpcf7dtx-';
|
||||
$url = plugin_dir_url(WPCF7DTX_FILE);
|
||||
$path = plugin_dir_path(WPCF7DTX_FILE);
|
||||
|
||||
wp_enqueue_style(
|
||||
$prefix . 'admin', //Handle
|
||||
$url . 'assets/styles/tag-generator.css', //Source
|
||||
array('contact-form-7-admin'), //Dependencies
|
||||
@filemtime($path . 'assets/styles/tag-generator.css') //Version
|
||||
);
|
||||
|
||||
//Plugin Scripts
|
||||
wp_enqueue_script(
|
||||
$prefix . 'taggenerator', //Handle
|
||||
$url . 'assets/scripts/tag-generator.js', //Source
|
||||
array('jquery', 'wpcf7-admin-taggenerator'), //Dependencies
|
||||
@filemtime($path . 'assets/scripts/tag-generator.js'), //Version
|
||||
true //In footer
|
||||
);
|
||||
}
|
||||
}
|
||||
add_action('admin_enqueue_scripts', 'wpcf7dtx_enqueue_admin_assets'); //Enqueue styles/scripts for admin page
|
||||
|
||||
/**
|
||||
* Create Tag Generators
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function wpcf7dtx_add_tag_generator_dynamictext()
|
||||
{
|
||||
if (!class_exists('WPCF7_TagGenerator')) {
|
||||
return;
|
||||
}
|
||||
$tag_generator = WPCF7_TagGenerator::get_instance();
|
||||
|
||||
//Dynamic Text Field
|
||||
$tag_generator->add(
|
||||
'dynamictext', //id
|
||||
__('dynamic text', 'contact-form-7-dynamic-text-extension'), //title
|
||||
'wpcf7dtx_tag_generator_dynamictext', //callback
|
||||
array('placeholder', 'readonly') //options
|
||||
);
|
||||
|
||||
//Dynamic Hidden Field
|
||||
$tag_generator->add(
|
||||
'dynamichidden', //id
|
||||
__('dynamic hidden', 'contact-form-7-dynamic-text-extension'), //title
|
||||
'wpcf7dtx_tag_generator_dynamictext' //callback
|
||||
);
|
||||
}
|
||||
add_action('wpcf7_admin_init', 'wpcf7dtx_add_tag_generator_dynamictext', 100);
|
||||
|
||||
/**
|
||||
* Echo HTML for Dynamic Tag Generator
|
||||
*
|
||||
* @param WPCF7_ContactForm $contact_form
|
||||
* @param array $options
|
||||
* @return void
|
||||
*/
|
||||
function wpcf7dtx_tag_generator_dynamictext($contact_form, $options = '')
|
||||
{
|
||||
$options = wp_parse_args($options);
|
||||
$type = $options['id'];
|
||||
switch ($type) {
|
||||
case 'dynamichidden': //hiden
|
||||
$description = __('Generate a form-tag for a hidden input field, with a dynamically generated default value.', 'contact-form-7-dynamic-text-extension');
|
||||
break;
|
||||
default:
|
||||
$description = __('Generate a form-tag for a single-line plain text input field, with a dynamically generated default value.', 'contact-form-7-dynamic-text-extension');
|
||||
break;
|
||||
}
|
||||
$utm_source = urlencode(home_url());
|
||||
$description .= sprintf(
|
||||
' %s <a href="https://aurisecreative.com/docs/contact-form-7-dynamic-text-extension/?utm_source=%s&utm_medium=link&utm_campaign=contact-form-7-dynamic-text-extension&utm_content=form-tag-generator-%s" target="_blank" rel="noopener">%s</a>.',
|
||||
__('For more details, see', 'contact-form-7-dynamic-text-extension'),
|
||||
esc_attr($utm_source), //UTM source
|
||||
esc_attr($type), //UTM content
|
||||
__('DTX knowledge base', 'contact-form-7-dynamic-text-extension')
|
||||
);
|
||||
|
||||
//Open Form-Tag Generator
|
||||
printf(
|
||||
'<div class="control-box"><fieldset><legend>%s</legend><table class="form-table"><tbody>',
|
||||
wp_kses($description, 'a') //Tag generator description
|
||||
);
|
||||
|
||||
//Input field - Required checkbox (not available for hidden fields)
|
||||
if ($type != 'dynamichidden') {
|
||||
printf(
|
||||
'<tr><th scope="row"><label for="%s">%s</label></th><td><label><input %s />%s</label></td></tr>',
|
||||
esc_attr($options['content'] . '-required'), // field id
|
||||
esc_html__('Field type', 'contact-form-7-dynamic-text-extension'), // field Label
|
||||
wpcf7_format_atts(array(
|
||||
'type' => 'checkbox',
|
||||
'name' => 'required',
|
||||
'id' => $options['content'] . '-required'
|
||||
)),
|
||||
esc_html__('Required field', 'contact-form-7-dynamic-text-extension') // checkbox label
|
||||
);
|
||||
}
|
||||
|
||||
//Input field - Field Name
|
||||
printf(
|
||||
'<tr><th scope="row"><label for="%s">%s</label></th><td><input %s /></td></tr>',
|
||||
esc_attr($options['content'] . '-name'), // field id
|
||||
esc_html__('Name', 'contact-form-7-dynamic-text-extension'), // field label
|
||||
wpcf7_format_atts(array(
|
||||
'type' => 'text',
|
||||
'name' => 'name',
|
||||
'id' => $options['content'] . '-name',
|
||||
'class' => 'tg-name oneline'
|
||||
))
|
||||
);
|
||||
|
||||
//Input field - Dynamic value
|
||||
printf(
|
||||
'<tr><th scope="row"><label for="%s">%s</label></th><td><input %s /><br /><small>%s <a href="https://aurisecreative.com/docs/contact-form-7-dynamic-text-extension/shortcodes/?utm_source=%s&utm_medium=link&utm_campaign=contact-form-7-dynamic-text-extension&utm_content=form-tag-generator-%s" target="_blank" rel="noopener">%s</a></small></td></tr>',
|
||||
esc_attr($options['content'] . '-values'), // field id
|
||||
esc_html__('Dynamic value', 'contact-form-7-dynamic-text-extension'), // field label
|
||||
wpcf7_format_atts(array(
|
||||
'type' => 'text',
|
||||
'name' => 'values',
|
||||
'id' => $options['content'] . '-values',
|
||||
'class' => 'oneline',
|
||||
'placeholder' => "CF7_GET key='foo'"
|
||||
)),
|
||||
esc_html__('Can be static text or a shortcode.', 'contact-form-7-dynamic-text-extension'),
|
||||
esc_attr($utm_source), //UTM source
|
||||
esc_attr($type), //UTM content
|
||||
esc_html__('View DTX shortcode syntax documentation', 'contact-form-7-dynamic-text-extension') //Link label
|
||||
);
|
||||
|
||||
//Input field - Dynamic placeholder (not available for hidden fields)
|
||||
if ($type != 'dynamichidden') {
|
||||
printf(
|
||||
'<tr><th scope="row"><label for="%s">%s</label></th><td><input %s /><input %s /><br /><small>%s <a href="https://aurisecreative.com/docs/contact-form-7-dynamic-text-extension/shortcodes/dtx-attribute-placeholder/?utm_source=%s&utm_medium=link&utm_campaign=contact-form-7-dynamic-text-extension&utm_content=form-tag-generator-%s" target="_blank" rel="noopener">%s</a></small></td></tr>',
|
||||
esc_attr($options['content'] . '-placeholder'), // field id
|
||||
esc_html__('Dynamic placeholder', 'contact-form-7-dynamic-text-extension'), // field label
|
||||
wpcf7_format_atts(array(
|
||||
'type' => 'hidden',
|
||||
'name' => 'placeholder',
|
||||
'class' => 'option'
|
||||
)),
|
||||
wpcf7_format_atts(array(
|
||||
'type' => 'text',
|
||||
'name' => 'dtx-placeholder',
|
||||
'id' => $options['content'] . '-placeholder', // field id
|
||||
'class' => 'oneline dtx-option',
|
||||
'placeholder' => 'CF7_get_post_var key=\'post_title\''
|
||||
)),
|
||||
esc_html__('Can be static text or a shortcode.', 'contact-form-7-dynamic-text-extension'),
|
||||
esc_attr($utm_source), //UTM source
|
||||
esc_attr($type), //UTM content
|
||||
esc_html__('View DTX placeholder documentation', 'contact-form-7-dynamic-text-extension') //Link label
|
||||
);
|
||||
}
|
||||
|
||||
//Input field - ID attribute
|
||||
printf(
|
||||
'<tr><th scope="row"><label for="%s">%s</label></th><td><input %s /></td></tr>',
|
||||
esc_attr($options['content'] . '-id'), // field id
|
||||
esc_html__('Id attribute', 'contact-form-7-dynamic-text-extension'), // field label
|
||||
wpcf7_format_atts(array(
|
||||
'type' => 'text',
|
||||
'name' => 'id',
|
||||
'id' => $options['content'] . '-id', // field id
|
||||
'class' => 'idvalue oneline option'
|
||||
))
|
||||
);
|
||||
|
||||
//Input field - Class attribute
|
||||
printf(
|
||||
'<tr><th scope="row"><label for="%s">%s</label></th><td><input %s /></td></tr>',
|
||||
esc_attr($options['content'] . '-class'), // field id
|
||||
esc_html__('Class attribute', 'contact-form-7-dynamic-text-extension'), // field label
|
||||
wpcf7_format_atts(array(
|
||||
'type' => 'text',
|
||||
'name' => 'class',
|
||||
'id' => $options['content'] . '-class', // field id
|
||||
'class' => 'classvalue oneline option'
|
||||
))
|
||||
);
|
||||
|
||||
//Input field - Readonly attribute (not available for hidden fields)
|
||||
if ($type != 'dynamichidden') {
|
||||
printf(
|
||||
'<tr><th scope="row"><label for="%s">%s</label></th><td><label><input %s />%s</label></td></tr>',
|
||||
esc_attr($options['content'] . '-readonly'), // field id
|
||||
esc_html__('Read only attribute', 'contact-form-7-dynamic-text-extension'), // field Label
|
||||
wpcf7_format_atts(array(
|
||||
'type' => 'checkbox',
|
||||
'name' => 'readonly',
|
||||
'id' => $options['content'] . '-readonly',
|
||||
'class' => 'readonlyvalue option'
|
||||
)),
|
||||
esc_html__('Do not let users edit this field', 'contact-form-7-dynamic-text-extension') // checkbox label
|
||||
);
|
||||
}
|
||||
|
||||
//Close Form-Tag Generator
|
||||
printf(
|
||||
'</tbody></table></fieldset></div><div class="insert-box"><input type="text" name="%s" class="tag code" readonly="readonly" onfocus="this.select()" /><div class="submitbox"><input type="button" class="button button-primary insert-tag" value="%s" /></div><br class="clear" /></div>',
|
||||
esc_attr($type),
|
||||
esc_html__('Insert Tag', 'contact-form-7-dynamic-text-extension')
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,358 @@
|
||||
<?php
|
||||
|
||||
/*****************************************************
|
||||
* Included Shortcodes
|
||||
*
|
||||
* See documentation for usage:
|
||||
* https://aurisecreative.com/docs/contact-form-7-dynamic-text-extension/shortcodes/
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
/**
|
||||
* Initialise DTX included shortcodes
|
||||
*
|
||||
* Hooked to `init`
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function wpcf7dtx_init_shortcodes()
|
||||
{
|
||||
add_shortcode('CF7_GET', 'wpcf7dtx_get');
|
||||
add_shortcode('CF7_POST', 'wpcf7dtx_post');
|
||||
add_shortcode('CF7_URL', 'wpcf7dtx_url');
|
||||
add_shortcode('CF7_referrer', 'wpcf7dtx_referrer');
|
||||
add_shortcode('CF7_bloginfo', 'wpcf7dtx_bloginfo');
|
||||
add_shortcode('CF7_get_post_var', 'wpcf7dtx_get_post_var');
|
||||
add_shortcode('CF7_get_custom_field', 'wpcf7dtx_get_custom_field');
|
||||
add_shortcode('CF7_get_current_user', 'wpcf7dtx_get_current_user');
|
||||
add_shortcode('CF7_get_attachment', 'wpcf7dtx_get_attachment');
|
||||
add_shortcode('CF7_guid', 'wpcf7dtx_guid');
|
||||
}
|
||||
add_action('init', 'wpcf7dtx_init_shortcodes'); //Add init hook to add shortcodes
|
||||
|
||||
/**
|
||||
* Get Variable from $_GET Array
|
||||
*
|
||||
* @param array $atts Optional. An associative array of shortcode attributes. Default is an empty array.
|
||||
* @param string $content Optional. A string of content between the opening and closing tags. Default is an empty string.
|
||||
* @param string $tag Optional. The shortcode tag. Default is an empty string.
|
||||
*
|
||||
* @return string Output of the shortcode
|
||||
*/
|
||||
function wpcf7dtx_get($atts = array(), $content = '', $tag = '')
|
||||
{
|
||||
extract(shortcode_atts(array(
|
||||
'key' => 0,
|
||||
'obfuscate' => ''
|
||||
), array_change_key_case((array)$atts, CASE_LOWER)));
|
||||
$valid_key = (is_numeric($key) && intval($key) > -1) || (is_string($key) && !empty($key));
|
||||
if ($valid_key && is_array($_GET) && count($_GET) && array_key_exists($key, $_GET) && !empty($_GET[$key])) {
|
||||
$value = sanitize_text_field(strval($_GET[$key]));
|
||||
if ($obfuscate && !empty($value)) {
|
||||
return wpcf7dtx_obfuscate($value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Variable from $_POST Array
|
||||
*
|
||||
* @param array $atts Optional. An associative array of shortcode attributes. Default is an empty array.
|
||||
* @param string $content Optional. A string of content between the opening and closing tags. Default is an empty string.
|
||||
* @param string $tag Optional. The shortcode tag. Default is an empty string.
|
||||
*
|
||||
* @return string Output of the shortcode
|
||||
*/
|
||||
function wpcf7dtx_post($atts = array(), $content = '', $tag = '')
|
||||
{
|
||||
extract(shortcode_atts(array(
|
||||
'key' => '',
|
||||
'obfuscate' => ''
|
||||
), array_change_key_case((array)$atts, CASE_LOWER)));
|
||||
$valid_key = (is_numeric($key) && intval($key) > -1) || (is_string($key) && !empty($key));
|
||||
if ($valid_key && is_array($_POST) && count($_POST) && array_key_exists($key, $_POST) && !empty($_POST[$key])) {
|
||||
$value = sanitize_text_field(strval($_POST[$key]));
|
||||
if ($obfuscate && !empty($value)) {
|
||||
return wpcf7dtx_obfuscate($value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Current URL
|
||||
*
|
||||
* @param array $atts Optional. An associative array of shortcode attributes. Default is an empty array.
|
||||
* @param string $content Optional. A string of content between the opening and closing tags. Default is an empty string.
|
||||
* @param string $tag Optional. The shortcode tag. Default is an empty string.
|
||||
*
|
||||
* @return string Output of the shortcode
|
||||
*/
|
||||
function wpcf7dtx_url($atts = array(), $content = '', $tag = '') {
|
||||
|
||||
extract(shortcode_atts(array(
|
||||
'allowed_protocols' => 'http,https',
|
||||
'obfuscate' => '',
|
||||
'part' => '',
|
||||
), array_change_key_case((array)$atts, CASE_LOWER)));
|
||||
|
||||
$allowed_protocols = explode(',', sanitize_text_field($allowed_protocols));
|
||||
|
||||
// Build the full URL from the $_SERVER array
|
||||
$url = sprintf('http%s://', is_ssl() ? 's' : '');
|
||||
if (!empty($_SERVER['SERVER_PORT']) && intval($_SERVER['SERVER_PORT']) !== 80) {
|
||||
$url = $url . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
|
||||
} else {
|
||||
$url = $url . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
|
||||
}
|
||||
|
||||
// Determine the value to return
|
||||
$value = '';
|
||||
|
||||
// If an individual part is requested, get that specific value using parse_url()
|
||||
if( $part ){
|
||||
$part_constant_map = [
|
||||
'host' => PHP_URL_HOST,
|
||||
'query' => PHP_URL_QUERY,
|
||||
'path' => PHP_URL_PATH,
|
||||
// 'fragment' => PHP_URL_FRAGMENT, // Can't get fragment because it's not part of the $_SERVER array
|
||||
];
|
||||
if( isset( $part_constant_map[$part] ) ) {
|
||||
$value = sanitize_text_field(parse_url($url, $part_constant_map[$part]));
|
||||
}
|
||||
}
|
||||
// No part requested, return the whole thing
|
||||
else {
|
||||
$value = sanitize_url($url, $allowed_protocols);
|
||||
}
|
||||
|
||||
// Obfuscate if requested
|
||||
if ($obfuscate && !empty($value)) {
|
||||
return wpcf7dtx_obfuscate($value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Referrer
|
||||
*
|
||||
* @param array $atts Optional. An associative array of shortcode attributes. Default is an empty array.
|
||||
* @param string $content Optional. A string of content between the opening and closing tags. Default is an empty string.
|
||||
* @param string $tag Optional. The shortcode tag. Default is an empty string.
|
||||
*
|
||||
* @return string Output of the shortcode
|
||||
*/
|
||||
function wpcf7dtx_referrer($atts = array(), $content = '', $tag = '')
|
||||
{
|
||||
extract(shortcode_atts(array(
|
||||
'allowed_protocols' => 'http,https',
|
||||
'obfuscate' => ''
|
||||
), array_change_key_case((array)$atts, CASE_LOWER)));
|
||||
$allowed_protocols = explode(',', sanitize_text_field($allowed_protocols));
|
||||
$value = empty($_SERVER['HTTP_REFERER']) ? '' : sanitize_url($_SERVER['HTTP_REFERER'], $allowed_protocols);
|
||||
if ($obfuscate && !empty($value)) {
|
||||
return wpcf7dtx_obfuscate($value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Variable from Bloginfo
|
||||
*
|
||||
* See possible values: https://developer.wordpress.org/reference/functions/get_bloginfo/
|
||||
*
|
||||
* @param array $atts Optional. An associative array of shortcode attributes. Default is an empty array.
|
||||
* @param string $content Optional. A string of content between the opening and closing tags. Default is an empty string.
|
||||
* @param string $tag Optional. The shortcode tag. Default is an empty string.
|
||||
*
|
||||
* @return string Output of the shortcode
|
||||
*/
|
||||
function wpcf7dtx_bloginfo($atts = array(), $content = '', $tag = '')
|
||||
{
|
||||
extract(shortcode_atts(array(
|
||||
'show' => 'name', //Backwards compatibility
|
||||
'key' => 'name',
|
||||
'obfuscate' => ''
|
||||
), array_change_key_case((array)$atts, CASE_LOWER)));
|
||||
$key = $show != $key && $show != 'name' ? $show : $key; //Use old value of "show" if not set to default value
|
||||
$value = sanitize_text_field(strval(get_bloginfo($key)));
|
||||
if ($obfuscate && !empty($value)) {
|
||||
return wpcf7dtx_obfuscate($value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Variable from a Post Object
|
||||
*
|
||||
* @param array $atts Optional. An associative array of shortcode attributes. Default is an empty array.
|
||||
* @param string $content Optional. A string of content between the opening and closing tags. Default is an empty string.
|
||||
* @param string $tag Optional. The shortcode tag. Default is an empty string.
|
||||
*
|
||||
* @return string Output of the shortcode
|
||||
*/
|
||||
function wpcf7dtx_get_post_var($atts = array(), $content = '', $tag = '')
|
||||
{
|
||||
extract(shortcode_atts(array(
|
||||
'key' => 'post_title',
|
||||
'post_id' => '',
|
||||
'obfuscate' => ''
|
||||
), array_change_key_case((array)$atts, CASE_LOWER)));
|
||||
switch ($key) {
|
||||
case 'slug':
|
||||
$key = 'post_name';
|
||||
break;
|
||||
case 'title':
|
||||
$key = 'post_title';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
$post_id = wpcf7dtx_get_post_id($post_id);
|
||||
if ($post_id && is_string($key) && !empty($key)) {
|
||||
$value = sanitize_text_field(trim(strval(get_post_field($key, $post_id))));
|
||||
if ($obfuscate && !empty($value)) {
|
||||
return wpcf7dtx_obfuscate($value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Value from Post Meta Field
|
||||
*
|
||||
* @param array $atts Optional. An associative array of shortcode attributes. Default is an empty array.
|
||||
* @param string $content Optional. A string of content between the opening and closing tags. Default is an empty string.
|
||||
* @param string $tag Optional. The shortcode tag. Default is an empty string.
|
||||
*
|
||||
* @return string Output of the shortcode
|
||||
*/
|
||||
function wpcf7dtx_get_custom_field($atts = array(), $content = '', $tag = '')
|
||||
{
|
||||
extract(shortcode_atts(array(
|
||||
'key' => '',
|
||||
'post_id' => '',
|
||||
'obfuscate' => ''
|
||||
), array_change_key_case((array)$atts, CASE_LOWER)));
|
||||
$post_id = wpcf7dtx_get_post_id($post_id);
|
||||
if ($post_id && is_string($key) && !empty($key)) {
|
||||
$value = get_post_meta($post_id, $key, true);
|
||||
if ($obfuscate && !empty($value)) {
|
||||
return wpcf7dtx_obfuscate($value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Value from Current User
|
||||
*
|
||||
* Retreives data from the `users` and `usermeta` tables.
|
||||
* Documentation: https://developer.wordpress.org/reference/classes/wp_user/get/
|
||||
*
|
||||
* @param array $atts Optional. An associative array of shortcode attributes. Default is an empty array.
|
||||
* @param string $content Optional. A string of content between the opening and closing tags. Default is an empty string.
|
||||
* @param string $tag Optional. The shortcode tag. Default is an empty string.
|
||||
*
|
||||
* @return string Output of the shortcode
|
||||
*/
|
||||
function wpcf7dtx_get_current_user($atts = array(), $content = '', $tag = '')
|
||||
{
|
||||
extract(shortcode_atts(array(
|
||||
'key' => 'user_login',
|
||||
'obfuscate' => ''
|
||||
), array_change_key_case((array)$atts, CASE_LOWER)));
|
||||
if (is_user_logged_in()) {
|
||||
$user = wp_get_current_user();
|
||||
$value = $user->get($key);
|
||||
if ($obfuscate && !empty($value)) {
|
||||
return wpcf7dtx_obfuscate($value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Attachment
|
||||
*
|
||||
* Retreives an attachment ID or absolute URL depending on attributes
|
||||
*
|
||||
* @since 3.1.0
|
||||
*
|
||||
* @param array $atts Optional. An associative array of shortcode attributes. Default is an empty array.
|
||||
* @param string $content Optional. A string of content between the opening and closing tags. Default is an empty string.
|
||||
* @param string $tag Optional. The shortcode tag. Default is an empty string.
|
||||
*
|
||||
* @return string Output of the shortcode
|
||||
*/
|
||||
function wpcf7dtx_get_attachment($atts = array(), $content = '', $tag = '')
|
||||
{
|
||||
extract(shortcode_atts(array(
|
||||
'id' => '', //Get attachment by ID
|
||||
'size' => 'full', //Define attachment size
|
||||
'post_id' => '', //If attachment ID is empty but post ID is not, get the featured image
|
||||
'return' => 'url', //Options are `id` or `url`
|
||||
'obfuscate' => ''
|
||||
), array_change_key_case((array)$atts, CASE_LOWER)));
|
||||
|
||||
//No attachment ID was provided, check for post ID to get it's featured image
|
||||
if (empty($id)) {
|
||||
if ($post_id = sanitize_text_field(strval($post_id))) {
|
||||
//If a post ID was provided, get it's featured image
|
||||
if (is_numeric($post_id) && (int)$post_id > 0) {
|
||||
$id = get_post_thumbnail_id($post_id);
|
||||
}
|
||||
} else {
|
||||
//If no post ID was provided, get current featured image
|
||||
global $post;
|
||||
if (isset($post) && property_exists($post, 'ID') && is_numeric($post->ID)) {
|
||||
$id = get_post_thumbnail_id(intval($post->ID));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Get the value
|
||||
$value = '';
|
||||
if ($id) {
|
||||
$id = intval(sanitize_text_field(strval($id)));
|
||||
switch ($return) {
|
||||
case 'id': //Return the attachment ID
|
||||
$value = esc_attr($id);
|
||||
break;
|
||||
default: //Return attachment URL
|
||||
$url = wp_get_attachment_image_url(intval($id), sanitize_text_field(strval($size)));
|
||||
$value = $url ? esc_url($url) : '';
|
||||
break;
|
||||
}
|
||||
if ($obfuscate && !empty($value)) {
|
||||
return wpcf7dtx_obfuscate($value);
|
||||
}
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* GUID Field
|
||||
*
|
||||
* @since 3.1.0
|
||||
*
|
||||
* @param array $atts Optional. An associative array of shortcode attributes. Default is an empty array.
|
||||
* @param string $content Optional. A string of content between the opening and closing tags. Default is an empty string.
|
||||
* @param string $tag Optional. The shortcode tag. Default is an empty string.
|
||||
*
|
||||
* @return string Output of the shortcode
|
||||
*/
|
||||
function wpcf7dtx_guid()
|
||||
{
|
||||
if (function_exists('com_create_guid') === true) {
|
||||
return trim(com_create_guid(), '{}');
|
||||
}
|
||||
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Obfuscate a value
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return string obfuscated value
|
||||
*/
|
||||
function wpcf7dtx_obfuscate($value = '')
|
||||
{
|
||||
$return = '';
|
||||
$value = strval($value); //Force value to be string
|
||||
if (!empty($value)) {
|
||||
foreach (str_split($value) as $letter) {
|
||||
$return .= '&#' . ord($letter) . ';';
|
||||
}
|
||||
}
|
||||
return sanitize_text_field(trim($return));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Post ID
|
||||
*
|
||||
* @param mixed $post_id
|
||||
* @return int An integer value of the passed post ID or the post ID of the current `$post` global object. 0 on Failure.
|
||||
*/
|
||||
function wpcf7dtx_get_post_id($post_id)
|
||||
{
|
||||
$post_id = is_numeric($post_id) && (int)$post_id > 0 ? intval($post_id) : 0;
|
||||
if (!$post_id) {
|
||||
//No post ID was provided, look it up
|
||||
global $post;
|
||||
if (isset($post) && property_exists($post, 'ID')) {
|
||||
$post_id = $post->ID;
|
||||
}
|
||||
}
|
||||
return $post_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a shortcode as deprecated and inform when it has been used.
|
||||
*
|
||||
* The current behavior is to trigger a user error if WP_DEBUG is true.
|
||||
*
|
||||
* This function is to be used in every function that is deprecated.
|
||||
*
|
||||
* @since 3.1.0
|
||||
* @access private
|
||||
*
|
||||
* @param string $tag The tag of the shortcode that was called.
|
||||
* @param string $version The version of the plugin that deprecated the shortcode.
|
||||
* @param string $replacement Optional. The shortcode that should have been used. Default null.
|
||||
*/
|
||||
function wpcf7dtx_deprecated_shortcode($tag, $version, $replacement = null, $documentation = null)
|
||||
{
|
||||
/**
|
||||
* Filter whether to trigger an error for deprecated shortcodes.
|
||||
*
|
||||
* @since 3.1.0
|
||||
*
|
||||
* @param bool $trigger Whether to trigger the error for deprecated functions. Default true.
|
||||
*/
|
||||
if (WP_DEBUG && apply_filters('deprecated_function_trigger_error', true)) {
|
||||
if (!is_null($replacement)) {
|
||||
if (!is_null($documentation)) {
|
||||
trigger_error(sprintf(
|
||||
__('%1$s is <strong>deprecated</strong> since version %2$s! Use Contact Form 7\'s built-in attribute "%3$s" instead. Contact Form 7 Documentation: %4$s', 'contact-form-7-dynamic-text-extension'),
|
||||
$tag,
|
||||
$version,
|
||||
$replacement,
|
||||
$documentation
|
||||
));
|
||||
} else {
|
||||
trigger_error(sprintf(
|
||||
__('%1$s is <strong>deprecated</strong> since version %2$s! Use Contact Form 7\'s built-in attribute "%3$s" instead.', 'contact-form-7-dynamic-text-extension'),
|
||||
$tag,
|
||||
$version,
|
||||
$replacement
|
||||
));
|
||||
}
|
||||
} else {
|
||||
trigger_error(sprintf(
|
||||
__('%1$s is <strong>deprecated</strong> since version %2$s with no alternative currently available.', 'contact-form-7-dynamic-text-extension'),
|
||||
$tag,
|
||||
$version
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse Content for Specified Shortcodes
|
||||
*
|
||||
* Parse a string of content for a specific shortcode to retrieve its attributes and content
|
||||
*
|
||||
* @since 3.1.0
|
||||
*
|
||||
* @param string $content The content to parse
|
||||
* @param string $tag The shortcode tag
|
||||
*
|
||||
* @return array An associative array with `tag` (string) and `shortcodes` (sequential array). If shortcodes were discovered, each one has keys for `atts` (associative array) and `content` (string)
|
||||
*/
|
||||
function wpcf7dtx_get_shortcode_atts($content)
|
||||
{
|
||||
$return = array(
|
||||
'tag' => '',
|
||||
'atts' => array()
|
||||
);
|
||||
//Search for shortcodes with attributes
|
||||
if (false !== ($start = strpos($content, ' '))) {
|
||||
$return['tag'] = substr($content, 0, $start); //Opens the start tag, assumes there are attributes because of the space
|
||||
|
||||
//Parse for shortcode attributes: `shortcode att1='foo' att2='bar'`
|
||||
|
||||
//Chop only the attributes e.g. `att1="foo" att2="bar"`
|
||||
$atts_str = trim(str_replace($return['tag'], '', $content));
|
||||
if (strpos($atts_str, "'") !== false) {
|
||||
$atts = explode("' ", substr(
|
||||
$atts_str,
|
||||
0,
|
||||
-1 //Clip off the last character, which is a single quote
|
||||
));
|
||||
if (is_array($atts) && count($atts)) {
|
||||
foreach ($atts as $att_str) {
|
||||
$pair = explode("='", $att_str);
|
||||
if (is_array($pair) && count($pair) > 1) {
|
||||
$key = sanitize_key(trim($pair[0])); //Validate & normalize the key
|
||||
if (!empty($key)) {
|
||||
$return['atts'][$key] = sanitize_text_field(html_entity_decode($pair[1]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Array Key Exists and Has Value
|
||||
*
|
||||
* @since 3.1.0
|
||||
*
|
||||
* @param string|int $key The key to search for in the array.
|
||||
* @param array $array The array to search.
|
||||
* @param mixed $default The default value to return if not found or is empty. Default is an empty string.
|
||||
*
|
||||
* @return mixed The value of the key found in the array if it exists or the value of `$default` if not found or is empty.
|
||||
*/
|
||||
function wpcf7dtx_array_has_key($key, $array = array(), $default = '')
|
||||
{
|
||||
//Check if this key exists in the array
|
||||
$valid_key = (is_string($key) && !empty($key)) || is_numeric($key);
|
||||
$valid_array = is_array($array) && count($array);
|
||||
if ($valid_key && $valid_array && array_key_exists($key, $array)) {
|
||||
//Always return if it's a boolean or number, otherwise only return it if it has any value
|
||||
if ($array[$key] || is_bool($array[$key]) || is_numeric($array[$key])) {
|
||||
return $array[$key];
|
||||
}
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
if (!function_exists('array_key_first')) {
|
||||
/**
|
||||
* Gets the first key of an array
|
||||
*
|
||||
* Gets the first key of the given array without affecting the internal array pointer.
|
||||
*
|
||||
* @param array $array
|
||||
* @return int|string|null
|
||||
*/
|
||||
function array_key_first($array = array())
|
||||
{
|
||||
foreach ($array as $key => $value) {
|
||||
return $key;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user