Changed source root directory

This commit is contained in:
2026-03-05 16:30:11 +01:00
parent dc85447ee1
commit 538f85d7a2
5868 changed files with 749734 additions and 99 deletions

View File

@@ -0,0 +1,38 @@
<?php
function flamingo_akismet_submit_spam( $comment ) {
return flamingo_akismet_submit( $comment, 'spam' );
}
function flamingo_akismet_submit_ham( $comment ) {
return flamingo_akismet_submit( $comment, 'ham' );
}
function flamingo_akismet_submit( $comment, $spam_or_ham = 'spam' ) {
if ( ! flamingo_akismet_is_active() ) {
return false;
}
if ( ! in_array( $spam_or_ham, array( 'spam', 'ham' ) ) ) {
return false;
}
$query_string = '';
foreach ( (array) $comment as $key => $data ) {
$query_string .=
$key . '=' . urlencode( wp_unslash( (string) $data ) ) . '&';
}
$response = Akismet::http_post( $query_string, 'submit-' . $spam_or_ham );
return (bool) $response[1];
}
function flamingo_akismet_is_active() {
if ( is_callable( array( 'Akismet', 'get_api_key' ) ) ) {
return (bool) Akismet::get_api_key();
}
return false;
}

View File

@@ -0,0 +1,27 @@
<?php
add_filter( 'map_meta_cap', 'flamingo_map_meta_cap', 10, 4 );
function flamingo_map_meta_cap( $caps, $cap, $user_id, $args ) {
$meta_caps = array(
'flamingo_edit_contact' => 'edit_users',
'flamingo_edit_contacts' => 'edit_users',
'flamingo_delete_contact' => 'edit_users',
'flamingo_edit_inbound_message' => 'edit_users',
'flamingo_edit_inbound_messages' => 'edit_users',
'flamingo_delete_inbound_message' => 'edit_users',
'flamingo_delete_inbound_messages' => 'edit_users',
'flamingo_spam_inbound_message' => 'edit_users',
'flamingo_unspam_inbound_message' => 'edit_users',
);
$meta_caps = apply_filters( 'flamingo_map_meta_cap', $meta_caps );
$caps = array_diff( $caps, array_keys( $meta_caps ) );
if ( isset( $meta_caps[$cap] ) ) {
$caps[] = $meta_caps[$cap];
}
return $caps;
}

View File

@@ -0,0 +1,228 @@
<?php
class Flamingo_Contact {
const post_type = 'flamingo_contact';
const contact_tag_taxonomy = 'flamingo_contact_tag';
private static $found_items = 0;
private $id;
public $email;
public $name;
public $props = array();
public $tags = array();
public $last_contacted;
public static function register_post_type() {
register_post_type( self::post_type, array(
'labels' => array(
'name' => __( 'Flamingo Contacts', 'flamingo' ),
'singular_name' => __( 'Flamingo Contact', 'flamingo' ),
),
'rewrite' => false,
'query_var' => false,
) );
register_taxonomy( self::contact_tag_taxonomy, self::post_type, array(
'labels' => array(
'name' => __( 'Flamingo Contact Tags', 'flamingo' ),
'singular_name' => __( 'Flamingo Contact Tag', 'flamingo' ),
),
'public' => false,
'rewrite' => false,
'query_var' => false,
) );
}
public static function find( $args = '' ) {
$defaults = array(
'posts_per_page' => 10,
'offset' => 0,
'orderby' => 'ID',
'order' => 'ASC',
'meta_key' => '',
'meta_value' => '',
'post_status' => 'any',
'tax_query' => array(),
'contact_tag_id' => '',
);
$args = wp_parse_args( $args, $defaults );
$args['post_type'] = self::post_type;
if ( ! empty( $args['contact_tag_id'] ) ) {
$args['tax_query'][] = array(
'taxonomy' => self::contact_tag_taxonomy,
'terms' => $args['contact_tag_id'],
'field' => 'term_id',
);
}
$q = new WP_Query();
$posts = $q->query( $args );
self::$found_items = $q->found_posts;
$objs = array();
foreach ( (array) $posts as $post ) {
$objs[] = new self( $post );
}
return $objs;
}
public static function count( $args = '' ) {
if ( $args ) {
$args = wp_parse_args( $args, array(
'offset' => 0,
'post_status' => 'publish',
) );
self::find( $args );
}
return absint( self::$found_items );
}
public static function search_by_email( $email ) {
$objs = self::find( array(
'posts_per_page' => 1,
'orderby' => 'ID',
'meta_key' => '_email',
'meta_value' => $email,
) );
if ( empty( $objs ) ) {
return null;
}
return $objs[0];
}
public static function add( $args = '' ) {
$args = wp_parse_args( $args, array(
'email' => '',
'name' => '',
'props' => array(),
'last_contacted' => '0000-00-00 00:00:00',
) );
$args = apply_filters( 'flamingo_add_contact', $args );
if ( empty( $args['email'] ) or ! is_email( $args['email'] ) ) {
return;
}
$obj = self::search_by_email( $args['email'] );
if ( ! $obj ) {
$obj = new self();
$obj->email = $args['email'];
$obj->name = $args['name'];
$obj->props = (array) $args['props'];
}
if ( '0000-00-00 00:00:00' !== $args['last_contacted'] ) {
$obj->last_contacted = $args['last_contacted'];
} elseif ( $datetime = date_create_immutable( 'now', wp_timezone() ) ) {
$obj->last_contacted = $datetime->format( 'Y-m-d H:i:s' );
}
$obj->save();
return $obj;
}
public function __construct( $post = null ) {
if ( ! empty( $post ) and $post = get_post( $post ) ) {
$this->id = $post->ID;
$this->email = get_post_meta( $post->ID, '_email', true );
$this->name = get_post_meta( $post->ID, '_name', true );
$this->props = get_post_meta( $post->ID, '_props', true );
$this->last_contacted =
get_post_meta( $post->ID, '_last_contacted', true );
$terms = wp_get_object_terms( $this->id, self::contact_tag_taxonomy );
if ( ! empty( $terms ) and ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$this->tags[] = $term->name;
}
}
}
}
public function __get( $name ) {
if ( 'id' === $name ) {
return $this->id;
}
}
public function id() {
return $this->id;
}
public function save() {
$post_title = $this->email;
$post_name = strtr( $this->email, '@', '-' );
$fields = flamingo_array_flatten( $this->props );
$fields = array_merge( $fields, array( $this->email, $this->name ) );
$fields = array_filter( array_map( 'trim', $fields ) );
$fields = array_unique( $fields );
$post_content = implode( "\n", $fields );
$postarr = array(
'ID' => absint( $this->id ),
'post_type' => self::post_type,
'post_status' => 'publish',
'post_title' => $post_title,
'post_name' => $post_name,
'post_content' => $post_content,
);
$post_id = wp_insert_post( $postarr );
if ( $post_id ) {
$this->id = $post_id;
update_post_meta( $post_id, '_email', $this->email );
update_post_meta( $post_id, '_name', $this->name );
update_post_meta( $post_id, '_props', $this->props );
update_post_meta( $post_id, '_last_contacted', $this->last_contacted );
wp_set_object_terms( $this->id, $this->tags, self::contact_tag_taxonomy );
}
return $post_id;
}
public function get_prop( $name ) {
if ( 'name' == $name ) {
return $this->name;
}
if ( isset( $this->props[$name] ) ) {
return $this->props[$name];
}
return '';
}
public function delete() {
if ( empty( $this->id ) ) {
return;
}
if ( $post = wp_delete_post( $this->id, true ) ) {
$this->id = 0;
}
return (bool) $post;
}
}

View File

@@ -0,0 +1,452 @@
<?php
class Flamingo_Inbound_Message {
const post_type = 'flamingo_inbound';
const spam_status = 'flamingo-spam';
const channel_taxonomy = 'flamingo_inbound_channel';
private static $found_items = 0;
private $id;
public $channel;
public $submission_status;
public $subject;
public $from;
public $from_name;
public $from_email;
public $fields;
public $meta;
public $akismet;
public $recaptcha;
public $spam;
public $spam_log;
public $consent;
private $timestamp = null;
private $hash = null;
public static function register_post_type() {
register_post_type( self::post_type, array(
'labels' => array(
'name' => __( 'Flamingo Inbound Messages', 'flamingo' ),
'singular_name' => __( 'Flamingo Inbound Message', 'flamingo' ),
),
'rewrite' => false,
'query_var' => false,
) );
register_post_status( self::spam_status, array(
'label' => __( 'Spam', 'flamingo' ),
'public' => false,
'exclude_from_search' => true,
'show_in_admin_all_list' => false,
'show_in_admin_status_list' => true,
) );
register_taxonomy( self::channel_taxonomy, self::post_type, array(
'labels' => array(
'name' => __( 'Flamingo Inbound Message Channels', 'flamingo' ),
'singular_name' => __( 'Flamingo Inbound Message Channel', 'flamingo' ),
),
'public' => false,
'hierarchical' => true,
'rewrite' => false,
'query_var' => false,
) );
}
public static function find( $args = '' ) {
$defaults = array(
'posts_per_page' => 10,
'offset' => 0,
'orderby' => 'ID',
'order' => 'ASC',
'meta_key' => '',
'meta_value' => '',
'post_status' => 'any',
'tax_query' => array(),
'channel' => '',
'channel_id' => 0,
'hash' => '',
);
$args = wp_parse_args( $args, $defaults );
$args['post_type'] = self::post_type;
if ( ! empty( $args['channel_id'] ) ) {
$args['tax_query'][] = array(
'taxonomy' => self::channel_taxonomy,
'terms' => absint( $args['channel_id'] ),
'field' => 'term_id',
);
}
if ( ! empty( $args['channel'] ) ) {
$args['tax_query'][] = array(
'taxonomy' => self::channel_taxonomy,
'terms' => $args['channel'],
'field' => 'slug',
);
}
if ( ! empty( $args['hash'] ) ) {
$args['meta_query'][] = array(
'key' => '_hash',
'value' => $args['hash'],
);
}
$q = new WP_Query();
$posts = $q->query( $args );
self::$found_items = $q->found_posts;
$objs = array();
foreach ( (array) $posts as $post ) {
$objs[] = new self( $post );
}
return $objs;
}
public static function count( $args = '' ) {
if ( $args ) {
$args = wp_parse_args( $args, array(
'offset' => 0,
'channel' => '',
'channel_id' => 0,
'post_status' => 'publish',
) );
self::find( $args );
}
return absint( self::$found_items );
}
public static function add( $args = '' ) {
$args = wp_parse_args( $args, array(
'channel' => '',
'status' => '',
'subject' => '',
'from' => '',
'from_name' => '',
'from_email' => '',
'fields' => array(),
'meta' => array(),
'akismet' => array(),
'recaptcha' => array(),
'spam' => false,
'spam_log' => array(),
'consent' => array(),
'timestamp' => null,
'posted_data_hash' => null,
) );
$args = apply_filters( 'flamingo_add_inbound', $args );
$obj = new self();
$obj->channel = $args['channel'];
$obj->submission_status = $args['status'];
$obj->subject = $args['subject'];
$obj->from = $args['from'];
$obj->from_name = $args['from_name'];
$obj->from_email = $args['from_email'];
$obj->fields = $args['fields'];
$obj->meta = $args['meta'];
$obj->akismet = $args['akismet'];
$obj->recaptcha = $args['recaptcha'];
$obj->spam = $args['spam'];
$obj->spam_log = $args['spam_log'];
$obj->consent = $args['consent'];
if ( $args['timestamp'] ) {
$obj->timestamp = $args['timestamp'];
}
if ( $args['posted_data_hash'] ) {
$obj->hash = $args['posted_data_hash'];
}
$obj->save();
return $obj;
}
public function __construct( $post = null ) {
if ( ! empty( $post ) and $post = get_post( $post ) ) {
$this->id = $post->ID;
$this->subject = get_post_meta( $post->ID, '_subject', true );
$this->from = get_post_meta( $post->ID, '_from', true );
$this->from_name = get_post_meta( $post->ID, '_from_name', true );
$this->from_email = get_post_meta( $post->ID, '_from_email', true );
$this->fields = get_post_meta( $post->ID, '_fields', true );
if ( ! empty( $this->fields ) ) {
foreach ( (array) $this->fields as $key => $value ) {
$meta_key = sanitize_key( '_field_' . $key );
if ( metadata_exists( 'post', $post->ID, $meta_key ) ) {
$value = get_post_meta( $post->ID, $meta_key, true );
$this->fields[$key] = $value;
}
}
}
$this->submission_status = get_post_meta( $post->ID,
'_submission_status', true
);
$this->meta = get_post_meta( $post->ID, '_meta', true );
$this->akismet = get_post_meta( $post->ID, '_akismet', true );
$this->recaptcha = get_post_meta( $post->ID, '_recaptcha', true );
$this->spam_log = get_post_meta( $post->ID, '_spam_log', true );
$this->consent = get_post_meta( $post->ID, '_consent', true );
$terms = wp_get_object_terms( $this->id, self::channel_taxonomy );
if ( ! empty( $terms ) and ! is_wp_error( $terms ) ) {
$this->channel = $terms[0]->slug;
}
if ( self::spam_status == get_post_status( $post ) ) {
$this->spam = true;
} else {
$this->spam = isset( $this->akismet['spam'] ) && $this->akismet['spam'];
}
$this->hash = get_post_meta( $post->ID, '_hash', true );
}
}
public function __get( $name ) {
if ( 'id' === $name ) {
return $this->id;
}
}
public function id() {
return $this->id;
}
public function save() {
if ( ! empty( $this->subject ) ) {
$post_title = $this->subject;
} else {
$post_title = __( '(No Title)', 'flamingo' );
}
$post_content = array_merge(
(array) $this->fields,
(array) $this->consent,
(array) $this->meta
);
$post_content = flamingo_array_flatten( $post_content );
$post_content = array_filter( array_map( 'trim', $post_content ) );
$post_content = implode( "\n", $post_content );
$post_status = $this->spam ? self::spam_status : 'publish';
$postarr = array(
'ID' => absint( $this->id ),
'post_type' => self::post_type,
'post_status' => $post_status,
'post_title' => $post_title,
'post_content' => $post_content,
'post_date' => $this->get_post_date(),
);
if (
$this->timestamp and
$datetime = date_create( '@' . $this->timestamp )
) {
$datetime->setTimezone( wp_timezone() );
$postarr['post_date'] = $datetime->format( 'Y-m-d H:i:s' );
}
$post_id = wp_insert_post( $postarr );
if ( $post_id ) {
$this->id = $post_id;
switch ( $post_status ) {
case self::spam_status:
update_post_meta( $post_id, '_spam_meta_time', time() );
break;
case 'trash':
// Do nothing.
break;
default:
delete_post_meta( $post_id, '_spam_meta_time' );
}
update_post_meta( $post_id, '_submission_status',
$this->submission_status
);
update_post_meta( $post_id, '_subject', $this->subject );
update_post_meta( $post_id, '_from', $this->from );
update_post_meta( $post_id, '_from_name', $this->from_name );
update_post_meta( $post_id, '_from_email', $this->from_email );
foreach ( $this->fields as $key => $value ) {
$meta_key = sanitize_key( '_field_' . $key );
update_post_meta( $post_id, $meta_key, $value );
$this->fields[$key] = null;
}
update_post_meta( $post_id, '_fields', $this->fields );
update_post_meta( $post_id, '_meta', $this->meta );
update_post_meta( $post_id, '_akismet', $this->akismet );
update_post_meta( $post_id, '_recaptcha', $this->recaptcha );
update_post_meta( $post_id, '_spam_log', $this->spam_log );
update_post_meta( $post_id, '_consent', $this->consent );
update_post_meta( $post_id, '_hash', $this->hash );
if ( term_exists( $this->channel, self::channel_taxonomy ) ) {
wp_set_object_terms(
$this->id,
$this->channel,
self::channel_taxonomy
);
}
}
return $post_id;
}
private function get_post_date() {
if ( empty( $this->id ) ) {
return false;
}
$post = get_post( $this->id );
if ( ! $post ) {
return false;
}
return $post->post_date;
}
public function trash() {
if ( empty( $this->id ) ) {
return;
}
if ( ! EMPTY_TRASH_DAYS ) {
return $this->delete();
}
$post = wp_trash_post( $this->id );
return (bool) $post;
}
public function untrash() {
if ( empty( $this->id ) ) {
return;
}
$post = wp_untrash_post( $this->id );
return (bool) $post;
}
public function delete() {
if ( empty( $this->id ) ) {
return;
}
if ( $post = wp_delete_post( $this->id, true ) ) {
$this->id = 0;
}
return (bool) $post;
}
public function spam() {
if ( $this->spam ) {
return;
}
$this->akismet_submit_spam();
$this->spam = true;
$user_name = get_user_option( 'user_login' );
if ( false === $user_name ) {
$user_name = __( 'Unknown', 'flamingo' );
}
if ( empty( $this->spam_log ) ) {
$this->spam_log = array();
}
$this->spam_log[] = array(
'agent' => 'flamingo',
'reason' => sprintf(
/* translators: %s: WordPress user name */
__( '%s has marked this message as spam.', 'flamingo' ),
$user_name
),
);
return $this->save();
}
public function akismet_submit_spam() {
if ( empty( $this->id ) or empty( $this->akismet ) ) {
return;
}
if ( isset( $this->akismet['spam'] ) and $this->akismet['spam'] ) {
return;
}
if ( empty( $this->akismet['comment'] ) ) {
return;
}
if ( flamingo_akismet_submit_spam( $this->akismet['comment'] ) ) {
$this->akismet['spam'] = true;
update_post_meta( $this->id, '_akismet', $this->akismet );
return true;
}
}
public function unspam() {
if ( ! $this->spam ) {
return;
}
$this->akismet_submit_ham();
$this->spam = false;
return $this->save();
}
public function akismet_submit_ham() {
if ( empty( $this->id ) or empty( $this->akismet ) ) {
return;
}
if ( isset( $this->akismet['spam'] ) and ! $this->akismet['spam'] ) {
return;
}
if ( empty( $this->akismet['comment'] ) ) {
return;
}
if ( flamingo_akismet_submit_ham( $this->akismet['comment'] ) ) {
$this->akismet['spam'] = false;
update_post_meta( $this->id, '_akismet', $this->akismet );
return true;
}
}
}

View File

@@ -0,0 +1,79 @@
<?php
/**
* Module for WordPress comments handling
*/
add_action( 'wp_insert_comment', 'flamingo_insert_comment', 10, 1 );
/**
* Creates a Flamingo_Contact record for the given comment.
*/
function flamingo_insert_comment( $comment_id ) {
$comment = get_comment( $comment_id );
if ( 1 !== (int) $comment->comment_approved ) {
return;
}
Flamingo_Contact::add( array(
'email' => $comment->comment_author_email,
'name' => $comment->comment_author,
'channel' => 'comment',
) );
}
add_action( 'transition_comment_status',
'flamingo_transition_comment_status',
10, 3
);
/**
* Creates a Flamingo_Contact record when the comment status changes.
*/
function flamingo_transition_comment_status( $new_status, $old_status, $comment ) {
if ( 'approved' !== $new_status ) {
return;
}
$email = $comment->comment_author_email;
$name = $comment->comment_author;
Flamingo_Contact::add( array(
'email' => $email,
'name' => $name,
'channel' => 'comment',
) );
}
add_action( 'activate_' . FLAMINGO_PLUGIN_BASENAME,
'flamingo_collect_contacts_from_comments',
10, 0
);
/**
* Creates Flamingo_Contact records for existing comments.
*/
function flamingo_collect_contacts_from_comments() {
$comments = get_comments( array(
'status' => 'approve',
'type' => 'comment',
'number' => 20,
) );
foreach ( $comments as $comment ) {
$email = $comment->comment_author_email;
$name = $comment->comment_author;
if ( empty( $email ) ) {
continue;
}
Flamingo_Contact::add( array(
'email' => $email,
'name' => $name,
'channel' => 'comment',
) );
}
}

View File

@@ -0,0 +1,46 @@
<?php
/**
* Cron job functionality
*
* @since 2.1
*/
add_action( 'admin_init', 'flamingo_schedule_activation', 10, 0 );
/**
* Creates a scheduled event, if it does not exist.
*
* @since 2.1
*/
function flamingo_schedule_activation() {
if ( ! wp_next_scheduled( 'flamingo_hourly_cron_job' ) ) {
wp_schedule_event( time(), 'hourly', 'flamingo_hourly_cron_job' );
}
}
add_action( 'flamingo_hourly_cron_job', 'flamingo_schedule_function', 10, 0 );
/**
* The cron job.
*
* @since 2.1
*/
function flamingo_schedule_function() {
flamingo_schedule_move_trash();
}
// Unscheduling cron jobs on plugin deactivation.
register_deactivation_hook( FLAMINGO_PLUGIN, 'flamingo_schedule_deactivate' );
/**
* Unschedules cron jobs.
*
* @since 2.1
*/
function flamingo_schedule_deactivate() {
wp_clear_scheduled_hook( 'flamingo_hourly_cron_job' );
wp_clear_scheduled_hook( 'flamingo_daily_cron_job' );
}

View File

@@ -0,0 +1,243 @@
<?php
abstract class Flamingo_CSV {
public function get_file_name() {
return 'flamingo.csv';
}
public function send_http_headers() {
$filename = $this->get_file_name();
$charset = get_option( 'blog_charset' );
header( "Content-Description: File Transfer" );
header( "Content-Disposition: attachment; filename=$filename" );
header( "Content-Type: text/csv; charset=$charset" );
}
public function print_data() {
echo '';
}
}
class Flamingo_Contact_CSV extends Flamingo_CSV {
public function get_file_name() {
return sprintf(
'%1$s-flamingo-contact-%2$s.csv',
sanitize_key( get_bloginfo( 'name' ) ),
wp_date( 'Y-m-d' )
);
}
public function print_data() {
$labels = array(
__( 'Email', 'flamingo' ),
__( 'Full name', 'flamingo' ),
__( 'First name', 'flamingo' ),
__( 'Last name', 'flamingo' ),
);
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo flamingo_csv_row( $labels );
$args = array(
'posts_per_page' => -1,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => '_email',
);
if ( ! empty( $_GET['s'] ) ) {
$args['s'] = $_GET['s'];
}
if ( ! empty( $_GET['orderby'] ) ) {
if ( 'email' === $_GET['orderby'] ) {
$args['meta_key'] = '_email';
} elseif ( 'name' === $_GET['orderby'] ) {
$args['meta_key'] = '_name';
}
}
if (
! empty( $_GET['order'] ) and
'asc' === strtolower( $_GET['order'] )
) {
$args['order'] = 'ASC';
}
if ( ! empty( $_GET['contact_tag_id'] ) ) {
$args['contact_tag_id'] = explode( ',', $_GET['contact_tag_id'] );
}
$items = Flamingo_Contact::find( $args );
foreach ( $items as $item ) {
echo "\r\n";
$row = array(
$item->email,
$item->get_prop( 'name' ),
$item->get_prop( 'first_name' ),
$item->get_prop( 'last_name' ),
);
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo flamingo_csv_row( $row );
}
}
}
class Flamingo_Inbound_CSV extends Flamingo_CSV {
public function get_file_name() {
return sprintf(
'%1$s-flamingo-inbound-%2$s.csv',
sanitize_key( get_bloginfo( 'name' ) ),
wp_date( 'Y-m-d' )
);
}
public function print_data() {
$args = array(
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
);
if ( ! empty( $_REQUEST['s'] ) ) {
$args['s'] = $_REQUEST['s'];
}
if ( ! empty( $_REQUEST['orderby'] ) ) {
if ( 'subject' === $_REQUEST['orderby'] ) {
$args['meta_key'] = '_subject';
$args['orderby'] = 'meta_value';
} elseif ( 'from' === $_REQUEST['orderby'] ) {
$args['meta_key'] = '_from';
$args['orderby'] = 'meta_value';
}
}
if (
! empty( $_REQUEST['order'] ) and
'asc' === strtolower( $_REQUEST['order'] )
) {
$args['order'] = 'ASC';
}
if ( ! empty( $_REQUEST['m'] ) ) {
$args['m'] = $_REQUEST['m'];
}
if ( ! empty( $_REQUEST['channel_id'] ) ) {
$args['channel_id'] = $_REQUEST['channel_id'];
}
if ( ! empty( $_REQUEST['channel'] ) ) {
$args['channel'] = $_REQUEST['channel'];
}
$items = Flamingo_Inbound_Message::find( $args );
if ( empty( $items ) ) {
return;
}
$labels = array_keys( $items[0]->fields );
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo flamingo_csv_row(
array_merge( $labels, array( __( 'Date', 'flamingo' ) ) )
);
foreach ( $items as $item ) {
echo "\r\n";
$row = array();
foreach ( $labels as $label ) {
$col = isset( $item->fields[$label] ) ? $item->fields[$label] : '';
if ( is_array( $col ) ) {
$col = flamingo_array_flatten( $col );
$col = array_filter( array_map( 'trim', $col ) );
$col = implode( ', ', $col );
}
$row[] = $col;
}
$row[] = get_post_time( 'c', false, $item->id() ); // Date
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo flamingo_csv_row( $row );
}
}
}
/**
* Retrieves text that represents a CSV row.
*/
function flamingo_csv_row( $inputs = array() ) {
$row = array();
foreach ( $inputs as $input ) {
$row[] = apply_filters( 'flamingo_csv_quotation', $input );
}
$separator = apply_filters( 'flamingo_csv_value_separator', ',' );
return implode( $separator, $row );
}
add_filter( 'flamingo_csv_quotation', 'flamingo_csv_quote', 10, 1 );
/**
* Retrieves text that represents a CSV cell with quotation.
*/
function flamingo_csv_quote( $input ) {
$prefix = apply_filters( 'flamingo_csv_field_prefix', '', $input );
$input = trim( sprintf( '%1$s %2$s', $prefix, $input ) );
return sprintf( '"%s"', str_replace( '"', '""', $input ) );
}
add_filter( 'flamingo_csv_field_prefix',
'flamingo_csv_field_prefix_text',
10, 2
);
/**
* Adds a security alert at the head of a cell.
*
* @see https://contactform7.com/2020/01/15/heads-up-about-spreadsheet-vulnerabilities/
*/
function flamingo_csv_field_prefix_text( $prefix, $input ) {
$formula_triggers = array( '=', '+', '-', '@' );
if ( in_array( substr( $input, 0, 1 ), $formula_triggers, true ) ) {
/* translators: %s: URL */
$prefix = __( '(Security Alert: Suspicious content is detected. See %s for details.)', 'flamingo' );
if ( in_array( substr( $prefix, 0, 1 ), $formula_triggers, true ) ) {
$prefix = '\'' . $prefix;
}
$prefix = sprintf(
$prefix,
esc_url( __( 'https://contactform7.com/heads-up-about-spreadsheet-vulnerabilities', 'flamingo' ) )
);
}
return $prefix;
}

View File

@@ -0,0 +1,17 @@
<?php
function flamingo_htmlize( $val ) {
$result = '';
if ( is_array( $val ) ) {
foreach ( $val as $v ) {
$result .= sprintf( '<li>%s</li>', flamingo_htmlize( $v ) );
}
$result = sprintf( '<ul>%s</ul>', $result );
} else {
$result = wpautop( esc_html( (string) $val ) );
}
return apply_filters( 'flamingo_htmlize', $result, $val );
}

View File

@@ -0,0 +1,60 @@
<?php
/**
* Retrieves a URL of a file under the Flamingo plugin directory.
*/
function flamingo_plugin_url( $path = '' ) {
$url = plugins_url( $path, FLAMINGO_PLUGIN );
if ( is_ssl() and 'http:' === substr( $url, 0, 5 ) ) {
$url = 'https:' . substr( $url, 5 );
}
return $url;
}
/**
* Converts a multidimensional array to a flat array.
*/
function flamingo_array_flatten( $input ) {
if ( ! is_array( $input ) ) {
return array( $input );
}
$output = array();
foreach ( $input as $value ) {
$output = array_merge( $output, flamingo_array_flatten( $value ) );
}
return $output;
}
/**
* Moves a spam to the Trash.
*
* @since 2.1
*/
function flamingo_schedule_move_trash() {
// abort if FLAMINGO_MOVE_TRASH_DAYS is set to zero or in minus
if ( (int) FLAMINGO_MOVE_TRASH_DAYS <= 0 ) {
return true;
}
$posts_to_move = Flamingo_Inbound_Message::find( array(
'posts_per_page' => 20,
'meta_key' => '_spam_meta_time',
'meta_value' => time() - ( DAY_IN_SECONDS * FLAMINGO_MOVE_TRASH_DAYS ),
'meta_compare' => '<',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'post_status' => Flamingo_Inbound_Message::spam_status,
) );
foreach ( $posts_to_move as $post ) {
$post->trash();
}
}

View File

@@ -0,0 +1,68 @@
<?php
/**
* Module for WordPress users handling
*/
add_action( 'profile_update', 'flamingo_user_profile_update', 10, 1 );
add_action( 'user_register', 'flamingo_user_profile_update', 10, 1 );
/**
* Creates a Flamingo_Contact record for the given user.
*/
function flamingo_user_profile_update( $user_id ) {
$user = new WP_User( $user_id );
$email = $user->user_email;
$name = $user->display_name;
$props = array(
'first_name' => $user->first_name,
'last_name' => $user->last_name,
);
if ( ! empty( $email ) ) {
Flamingo_Contact::add( array(
'email' => $email,
'name' => $name,
'props' => $props,
'channel' => 'user',
) );
}
}
add_action( 'activate_' . FLAMINGO_PLUGIN_BASENAME,
'flamingo_collect_contacts_from_users',
10, 0
);
/**
* Creates Flamingo_Contact records for existing users.
*/
function flamingo_collect_contacts_from_users() {
$users = get_users( array(
'number' => 20,
) );
foreach ( $users as $user ) {
$email = $user->user_email;
$name = $user->display_name;
if ( empty( $email ) ) {
continue;
}
$props = array(
'first_name' => empty( $user->first_name ) ? '' : $user->first_name,
'last_name' => empty( $user->last_name ) ? '' : $user->last_name,
);
Flamingo_Contact::add( array(
'email' => $email,
'name' => $name,
'props' => $props,
'channel' => 'user',
) );
}
}