Skip to content

Commit

Permalink
run phpcs on sample-code-snippets/ too
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Jan 11, 2025
1 parent 7ee776f commit d41f882
Show file tree
Hide file tree
Showing 111 changed files with 1,075 additions and 986 deletions.
3 changes: 3 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
<file>includes/</file>
<file>integrations/</file>
<file>config/</file>
<file>sample-code-snippets/</file>
<exclude-pattern>*\.(html|css|js|md|json|xml|sh)</exclude-pattern>
<rule ref="Generic.Arrays.DisallowLongArraySyntax.Found"/>
<rule ref="PSR12">
<exclude name="PSR1.Methods.CamelCapsMethodName" />
<exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace" />
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps" />
<exclude name="Generic.Files.LineLength.TooLong" />
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols" />
</rule>
</ruleset>
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
<?php

/**
* The snippet below hooks into the form with ID 500
* The snippet below hooks into the form with ID 500
*
* - If a subscriber was updated; it redirects straight to the download monitor download
* - If a subscriber was newly added; it redirects to another page where you can show another more detailed form. Make sure to set the "redirect on success" setting of the detailed form to your download.
*/
add_action( 'mc4wp_form_updated_subscriber', function( $form ) {
if( $form->ID != 500 ) {
return;
}

dlm_mailchimp_set_cookie( $form->data['EMAIL'] );
add_action('mc4wp_form_updated_subscriber', function ($form) {
if ($form->ID != 500) {
return;
}

dlm_mailchimp_set_cookie($form->data['EMAIL']);

// form was used to update a subscriber; redirect to download
wp_redirect( 'http://my-site.com/download-url' );
exit;
wp_redirect('http://my-site.com/download-url');
exit;
});

add_action( 'mc4wp_form_subscribed', function( $form ) {
if( $form->ID != 500 ) {
return;
}
add_action('mc4wp_form_subscribed', function ($form) {
if ($form->ID != 500) {
return;
}

// form was used to add a new subscriber; redirect to detailed form
wp_redirect( 'http://my-site.com/page-with-detailed-form' );
exit;
wp_redirect('http://my-site.com/page-with-detailed-form');
exit;
});

6 changes: 3 additions & 3 deletions sample-code-snippets/add-ons/top-bar/add-consent-checkbox.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
<?php

add_action( 'mctb_before_submit_button', function() {
echo '<input name="AGREE_TO_TERMS" type="checkbox" value="1" required=""> I have read and agree to the terms &amp; conditions';
add_action('mctb_before_submit_button', function () {
echo '<input name="AGREE_TO_TERMS" type="checkbox" value="1" required=""> I have read and agree to the terms &amp; conditions';
});
11 changes: 6 additions & 5 deletions sample-code-snippets/add-ons/top-bar/add-name-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
/**
* Echo a NAME field just before the submit button.
*/
add_action( 'mctb_before_submit_button', function() {
echo '<input type="text" name="NAME" placeholder="Your name" />';

add_action('mctb_before_submit_button', function () {
echo '<input type="text" name="NAME" placeholder="Your name" />';
});


Expand All @@ -13,7 +14,7 @@
*
* @param array $vars
*/
add_filter( 'mctb_data', function( $vars ) {
$vars['NAME'] = ( isset( $_POST['NAME'] ) ) ? sanitize_text_field( $_POST['NAME'] ) : '';
return $vars;
add_filter('mctb_data', function ($vars) {
$vars['NAME'] = ( isset($_POST['NAME']) ) ? sanitize_text_field($_POST['NAME']) : '';
return $vars;
});
6 changes: 3 additions & 3 deletions sample-code-snippets/add-ons/top-bar/add-subscriber-tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/**
* Echo a susbcriber tag
*/
add_filter( 'mctb_subscriber_data', function( $subscriber ) {

add_filter('mctb_subscriber_data', function ($subscriber) {
$subscriber->tags[] = 'My tag';
return $subscriber;
});
});
16 changes: 8 additions & 8 deletions sample-code-snippets/add-ons/top-bar/add-to-interest-groups.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

add_filter( 'mctb_data', function($data) {
add_filter('mctb_data', function ($data) {

// make sure we have an array to work with
if( ! isset( $data['INTERESTS'] ) ) {
$data['INTERESTS'] = array();
}
// make sure we have an array to work with
if (! isset($data['INTERESTS'])) {
$data['INTERESTS'] = [];
}

// replace "interest-id" with the actual ID of your interest.
$data['INTERESTS'][] = "interest-id";
// replace "interest-id" with the actual ID of your interest.
$data['INTERESTS'][] = "interest-id";

return $data;
return $data;
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

add_filter( 'mctb_show_bar', function() {
add_filter('mctb_show_bar', function () {
// don't show for posts with category "fruit"
if (is_single() && has_category(array('fruit'))) {
if (is_single() && has_category(['fruit'])) {
return false;
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
/**
* Set cookie when Top Bar is used to subscribe
*/
add_action( 'mctb_subscribed', function() {
$expires = time() + ( 60 * 60 * 24 * 30 ); // 30 days
setcookie( 'mctb_hide_bar', '1', $expires, '/' );

add_action('mctb_subscribed', function () {
$expires = time() + ( 60 * 60 * 24 * 30 ); // 30 days
setcookie('mctb_hide_bar', '1', $expires, '/');
});

/**
* Do not load Top Bar when cookie exists
*/
add_filter( 'mctb_show_bar', function( $show ) {
return $show && empty( $_COOKIE['mctb_hide_bar'] );
add_filter('mctb_show_bar', function ($show) {
return $show && empty($_COOKIE['mctb_hide_bar']);
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

add_action('wp_footer', function() {
?>
<script>
document.querySelector('#mailchimp-top-bar form').addEventListener('submit', function() {
// your code goes here
});
</script>
<?php
}, 20 );
add_action('wp_footer', function () {
?>
<script>
document.querySelector('#mailchimp-top-bar form').addEventListener('submit', function() {
// your code goes here
});
</script>
<?php
}, 20);
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
* Set cookie when Top Bar is used to subscribe
*/

add_filter( 'mctb_show_bar', function() {
add_filter('mctb_show_bar', function () {
return !isset($_COOKIE['mctb_bar_hidden']);
});
3 changes: 2 additions & 1 deletion sample-code-snippets/forms/add-class-to-form-element.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*
* @return array
*/
function myprefix_add_css_class_to_form(array $classes, MC4WP_Form $form) {
function myprefix_add_css_class_to_form(array $classes, MC4WP_Form $form)
{
$classes[] = 'my-class';
return $classes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
/**
* Insert a sign-up form after the 3rd paragraph.
*/
add_filter( 'the_content', function( $content ) {

if( is_single() ) {
$content = mc4wp_insert_after_paragraph( $content, mc4wp_get_form(), 2 );
}
add_filter('the_content', function ($content) {

return $content;
if (is_single()) {
$content = mc4wp_insert_after_paragraph($content, mc4wp_get_form(), 2);
}

return $content;
});


Expand All @@ -24,20 +25,21 @@
* @return string
*/

function mc4wp_insert_after_paragraph( $content, $insertion, $paragraph_number = 2 ) {
static $closing_p = '</p>';
function mc4wp_insert_after_paragraph($content, $insertion, $paragraph_number = 2)
{
static $closing_p = '</p>';

$paragraphs = explode( $closing_p, $content );
$new_content = '';
$target_index = $paragraph_number - 1;
$paragraphs = explode($closing_p, $content);
$new_content = '';
$target_index = $paragraph_number - 1;

foreach( $paragraphs as $index => $paragraph ) {
$new_content .= $paragraph;
foreach ($paragraphs as $index => $paragraph) {
$new_content .= $paragraph;

if( $index == $target_index ) {
$new_content .= $insertion;
}
}
if ($index == $target_index) {
$new_content .= $insertion;
}
}

return $new_content;
return $new_content;
}
13 changes: 6 additions & 7 deletions sample-code-snippets/forms/add-form-after-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
/**
* Insert a sign-up form after the post content.
*/
add_filter( 'the_content', function( $content ) {

if( is_single() ) {
$content .= mc4wp_get_form();
}

return $content;
});
add_filter('the_content', function ($content) {

if (is_single()) {
$content .= mc4wp_get_form();
}

return $content;
});
13 changes: 7 additions & 6 deletions sample-code-snippets/forms/blacklist-email-addresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
/**
* Block certain email addresses from signing up through forms.
*/
add_filter( 'mc4wp_form_errors', function( $errors, MC4WP_Form $form ) {

add_filter('mc4wp_form_errors', function ($errors, MC4WP_Form $form) {
$data = $form->get_data();
$email = strtolower( $data['EMAIL'] );
$email = strtolower($data['EMAIL']);

// add your blocked email addresses here.
$blocked_emails = array(
$blocked_emails = [
'[email protected]',
'[email protected]',
);
];

if( in_array( $email, $blocked_emails ) ) {
if (in_array($email, $blocked_emails)) {
$errors[] = 'spam';
}

return $errors;
}, 10, 2 );
}, 10, 2);
11 changes: 6 additions & 5 deletions sample-code-snippets/forms/blacklist-ip-addresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
/**
* Block certain IP addresses from signing up through forms.
*/
add_filter( 'mc4wp_form_errors', function( $errors ) {

add_filter('mc4wp_form_errors', function ($errors) {

// add your blocked IP Addresses here.
$blocked_ips = array(
$blocked_ips = [
'123.456.789.1'
);
];

if( in_array( $_SERVER['REMOTE_ADDR'], $blocked_ips ) ) {
if (in_array($_SERVER['REMOTE_ADDR'], $blocked_ips)) {
$errors[] = 'spam';
}

return $errors;
});
});
17 changes: 9 additions & 8 deletions sample-code-snippets/forms/combine-two-fields-into-one.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
*
* @return array
*/
function myprefix_combine_fields( $data, MC4WP_Form $form ) {
function myprefix_combine_fields($data, MC4WP_Form $form)
{

// get values for both fields
$field1 = ( isset( $data['FIELD_ONE'] ) ) ? $data['FIELD_ONE'] : '';
$field2 = ( isset( $data['FIELD_TWO'] ) ) ? $data['FIELD_TWO'] : '';
// get values for both fields
$field1 = ( isset($data['FIELD_ONE']) ) ? $data['FIELD_ONE'] : '';
$field2 = ( isset($data['FIELD_TWO']) ) ? $data['FIELD_TWO'] : '';

// merge the two fields into one
// merge the two fields into one
$data['COMBINED_FIELD'] = $field1 . ' ' . $field2;

// return customized data
return $data;
// return customized data
return $data;
}

add_filter( 'mc4wp_form_data', 'myprefix_combine_fields', 10, 2 );
add_filter('mc4wp_form_data', 'myprefix_combine_fields', 10, 2);
Loading

0 comments on commit d41f882

Please sign in to comment.