Skip to content

Instantly share code, notes, and snippets.

@samrocksc
Last active December 10, 2021 08:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save samrocksc/467bb16d20bbd3f27e340b6440743cff to your computer and use it in GitHub Desktop.
Save samrocksc/467bb16d20bbd3f27e340b6440743cff to your computer and use it in GitHub Desktop.
<?php
/**
* Clean up the_excerpt()
*/
function roots_excerpt_more() {
return ' &hellip; </p><p><a href="' . get_permalink() . '"><strong>' . __('Read More', 'roots') . '</strong></a>';
}
add_filter('excerpt_more', 'roots_excerpt_more');
function custom_excerpt($new_length = 20, $new_more = '...') {
// use the variable passed from $new_length as the length of the excerpt
add_filter('excerpt_length', function () use ($new_length) {
return $new_length;
}, 999);
// determine what comes at the end of the excerpt (in this case ...)
add_filter('excerpt_more', function () use ($new_more) {
return $new_more;
});
// generate the current excerpt
$output = get_the_excerpt();
// use wptexturize to basically sanitize the excerpt
$output = apply_filters('wptexturize', $output);
// convert_chars to remove metadata tags and convert others to unicode
$output = apply_filters('convert_chars', $output);
// wrap it back up in p tags
//$output = '&lt;p&gt;' . $output . '&lt;/p&gt;';
// the above line may not be needed depending on the status of wpautop
// echo that sucker
echo $output;
}
//Options Pages
if( function_exists('acf_add_options_sub_page') )
{
acf_add_options_sub_page( 'Social' );
}
// social links (for footer widget)
// [social-links]
add_shortcode('social-links', 'social_links');
/**
* Buffer get_template_part output to avoid it echoing and breaking the social links output
* @param string $social svg file name
* @return string returns get_template_part sans the echo
*/
function clean_get_template_part($social) {
ob_start();
get_template_part( '/assets/img/social', $social );
return ob_get_clean();
}
/**
* Create a list of social links and wraps them in HTML
* @return string $output contains the markup
*/
function social_links() {
$output = '<div class="social">';
if( have_rows('profiles', 'options') ):
while( have_rows('profiles', 'options') ): the_row();
$social = get_sub_field('network').'.svg';
$output .= '<a href="'. get_sub_field('profile_link').'" target="_blank">';
$output .= clean_get_template_part($social);
$output .= '</a>';
endwhile;
endif;
$output .= '</div>';
return $output;
}
// Gravity Form Filters
add_filter( 'gform_field_input', 'zipcode_pattern', 10, 5 );
function zipcode_pattern( $input, $field, $value, $lead_id, $form_id ) {
if ( $form_id == 1 && $field->id == 9 ) {
$input = '<div class="ginput_container"><input name="input_8" id="input_1_8" type="text" value="" class="medium" tabindex="8" pattern="\d*"></div>';
}
return $input;
}
//contact form event tracking
function add_conversion_tracking_code($button, $form) {
$dom = new DOMDocument();
$dom->loadHTML($button);
$input = $dom->getElementsByTagName('input')->item(0);
if ($input->hasAttribute('onclick')) {
$input->setAttribute("onclick","__gaTracker('send', 'event', { eventCategory: 'Forms', eventAction: 'Form Submission', eventLabel: 'Contact'});".$input->getAttribute("onclick"));
} else {
$input->setAttribute("onclick","__gaTracker('send', 'event', { eventCategory: 'Forms', eventAction: 'Form Submission', eventLabel: 'Contact'});");
}
return $dom->saveHtml();
}
add_filter( 'gform_submit_button_2', 'add_conversion_tracking_code', 10, 2);
/**
* Attach images uploaded through Gravity Form to ACF Gallery Field
*
* @author Joshua David Nelson, josh@joshuadnelson.com
* @return void
*/
add_filter( "gform_after_submission_4", 'jdn_set_post_acf_gallery_field', 10, 2 );
function jdn_set_post_acf_gallery_field( $entry, $form ) {
$gf_images_field_id_1 = 14; // the upload field id
$gf_images_field_id_2 = 15; // the upload field id
$gf_images_field_id_3 = 16; // the upload field id
$gf_images_field_id_4 = 17; // the upload field id
$image1_id = 'field_56b97d8144799'; // the acf gallery field id
$image2_id = 'field_56b97dc94479b'; // the acf gallery field id
$image3_id = 'field_56b97dc34479a'; // the acf gallery field id
$image4_id = 'field_56b97dd04479c'; // the acf gallery field id
// get post
if( isset( $entry['post_id'] ) ) {
$post = get_post( $entry['post_id'] );
if( is_null( $post ) )
return;
} else {
return;
}
// Clean up images upload and create array for gallery field
$images = '';
$image_id = NULL;
if( isset( $entry[ $gf_images_field_id_1 ] ) ) {
$images = stripslashes( $entry[ $gf_images_field_id_1 ] );
$images = explode('|:||:||:||:|', $images);
$image_id = $images[1];
}
// Update gallery field with array
if( ! empty( $image_id ) ) {
update_field( $image1_id, $image_id, $post->ID );
}
// Clean up images upload and create array for gallery field
$images = '';
$image_id = NULL;
if( isset( $entry[ $gf_images_field_id_2 ] ) ) {
$images = stripslashes( $entry[ $gf_images_field_id_2 ] );
$images = explode('|:||:||:||:|', $images);
$image_id = $images[1];
}
// Update gallery field with array
if( ! empty( $image_id ) ) {
update_field( $image2_id, $image_id, $post->ID );
}
// Clean up images upload and create array for gallery field
$images = '';
$image_id = NULL;
if( isset( $entry[ $gf_images_field_id_3 ] ) ) {
$images = stripslashes( $entry[ $gf_images_field_id_3 ] );
$images = explode('|:||:||:||:|', $images);
$image_id = $images[1];
}
// Update gallery field with array
if( ! empty( $image_id ) ) {
update_field( $image3_id, $image_id, $post->ID );
}
// Clean up images upload and create array for gallery field
$images = '';
$image_id = NULL;
if( isset( $entry[ $gf_images_field_id_4 ] ) ) {
$images = stripslashes( $entry[ $gf_images_field_id_4 ] );
$images = explode('|:||:||:||:|', $images);
$image_id = $images[1];
}
// Update gallery field with array
if( ! empty( $image_id ) ) {
update_field( $image4_id, $image_id, $post->ID );
}
// Updating post
wp_update_post( $post );
}
add_filter( 'gform_ajax_spinner_url', 'tgm_io_custom_gforms_spinner' );
/**
* Changes the default Gravity Forms AJAX spinner.
*
* @since 1.0.0
*
* @param string $src The default spinner URL.
* @return string $src The new spinner URL.
*/
function tgm_io_custom_gforms_spinner( $src ) {
return get_stylesheet_directory_uri() . '/assets/img/486.gif';
}
//Only Use this for DEV!!!!!
//add_filter( 'gf_salesforce_service_is_sandbox', '__return_true' );
add_action('gf_salesforce_connection', 'gf_salesforce_set_default_assignement_rule');
function gf_salesforce_set_default_assignement_rule($client) {
// Two Options for Setting Assignment Rule
// 1. Pass in AssignmentRule ID and "false" to use a specific assignment rule.
// 2. Pass in null and true to trigger the DEFAULT AssignementRule
$header = new \AssignmentRuleHeader(null, true);
$client->setAssignmentRuleHeader($header);
return $client;
}
//Gform Scripts in footer
add_filter("gform_init_scripts_footer", "init_scripts");
function init_scripts() {
return true;
}
//SVGs in Apply Form
add_filter('gform_field_content', __NAMESPACE__ . '\\svgs_for_gravityforms_fields', 10, 5);
function svgs_for_gravityforms_fields($content, $field, $value, $lead_id, $form_id){
if ($form_id == 7 ) {
if($field["type"] == 'checkbox' && $field['id'] == 5) {
$svg = file_get_contents( get_template_directory_uri() . '/assets/img/multihouse.svg');
$content = str_replace('<input name=\'input_5.1\' ', $svg . '<input name=\'input_5.1\' class=\'form-control\' ', $content);
$svg = file_get_contents( get_template_directory_uri() . '/assets/img/house.svg');
$content = str_replace('<input name=\'input_5.2\' ', $svg . '<input name=\'input_5.2\' class=\'form-control\' ', $content);
$svg = file_get_contents( get_template_directory_uri() . '/assets/img/credit.svg');
$content = str_replace('<input name=\'input_5.3\' ', $svg . '<input name=\'input_5.3\' class=\'form-control\' ', $content);
//$content = '<pre class="text-left">' . print_r($field, true) . '</pre>';
}
}
return $content;
}
// THIS IS A GRAVITY FORMS PLUGIN
// 1 - Tie our validation function to the 'gform_validation' hook
add_filter( 'gform_validation_7', 'validate_phone' );
function validate_phone( $validation_result ) {
// 2 - Get the form object from the validation result
$form = $validation_result['form'];
// 3 - Get the current page being validated
$current_page = rgpost( 'gform_source_page_number_' . $form['id'] ) ? rgpost( 'gform_source_page_number_' . $form['id'] ) : 1;
// 4 - Loop through the form fields
foreach( $form['fields'] as &$field ) {
// 5 - If the field does not have our designated CSS class, skip it
if ( strpos( $field->cssClass, 'validate-phone' ) === false ) {
continue;
}
// 6 - Get the field's page number
$field_page = $field->pageNumber;
// 7 - Check if the field is hidden by GF conditional logic
$is_hidden = RGFormsModel::is_field_hidden( $form, $field, array() );
// 8 - If the field is not on the current page OR if the field is hidden, skip it
if ( $field_page != $current_page || $is_hidden ) {
continue;
}
// 9 - Get the submitted value from the $_POST
$field_value = rgpost( "input_{$field['id']}" );
// 10 - Make a call to your validation function to validate the value
$is_valid = is_vin( $field_value );
// 11 - If the field is valid we don't need to do anything, skip it
if ( $is_valid ) {
continue;
}
// 12 - The field field validation, so first we'll need to fail the validation for the entire form
$validation_result['is_valid'] = false;
// 13 - Next we'll mark the specific field that failed and add a custom validation message
$field->failed_validation = true;
$field->validation_message = 'Please enter a valid telephone number';
}
// 14 - Assign our modified $form object back to the validation result
$validation_result['form'] = $form;
// 15 - Return the validation result
return $validation_result;
}
function is_vin( $vin ) {
$vin = strtoupper( trim( $vin ) );
preg_match('/5{3}/', $vin, $matches);
if($matches == null) {
return true;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment