'', 'name' => '', 'enabled' => true, 'post_type' => 'post', 'post_status' => 'draft', 'author' => 'current', 'author_id' => 0, 'title' => '', 'content' => '', 'excerpt' => '', 'featured_field' => '', 'taxonomies' => array(), 'meta' => array(), 'conditions' => array( 'enabled' => false, 'match' => 'all', 'rules' => array() ), 'update' => array( 'enabled' => false, 'meta_key' => '', 'meta_value' => '' ), // phpcs:ignore WordPress.DB.SlowDBQuery -- Settings array keys, not a query. ); } /* ------------------------------------------------------------------ * * Run * ------------------------------------------------------------------ */ /** * Create / update posts for an entry. * * @param array $entry Entry row. * @param array $values Values. * @param int $form_id Form id. */ public static function run( $entry, $values, $form_id ) { $entry = (array) $entry; $values = (array) $values; foreach ( self::feeds( $form_id ) as $feed ) { $feed = array_merge( self::blank(), (array) $feed ); if ( empty( $feed['enabled'] ) ) { continue; } if ( ! empty( $feed['conditions']['enabled'] ) && ! Conditions::passes( $feed['conditions'], $values ) ) { continue; } try { $res = self::create( $feed, $entry, $values, (int) $form_id ); } catch ( \Throwable $e ) { $res = new \WP_Error( 'fwren_post_exception', $e->getMessage() ); } $name = $feed['name'] ? $feed['name'] : __( 'Post feed', 'fieldwren' ); if ( is_wp_error( $res ) ) { Logs::add( $form_id, (int) ( $entry['id'] ?? 0 ), 'post', 'failed', $name, $res->get_error_message() ); continue; } if ( ! empty( $entry['id'] ) ) { Entries::add_meta( (int) $entry['id'], '_post_id', (int) $res, (int) $form_id ); } /* translators: %d post id */ Logs::add( $form_id, (int) ( $entry['id'] ?? 0 ), 'post', 'success', $name, sprintf( __( 'Post #%d saved.', 'fieldwren' ), (int) $res ) ); do_action( 'fwren/post_creation/saved', (int) $res, $feed, $entry, $values ); } } /** * One feed. * * @return int|\WP_Error Post id. */ private static function create( array $feed, array $entry, array $values, $form_id ) { $pt = sanitize_key( $feed['post_type'] ); if ( ! post_type_exists( $pt ) || Plugin::POST_TYPE === $pt || 'attachment' === $pt ) { return new \WP_Error( 'fwren_post_type', __( 'The post type does not exist.', 'fieldwren' ) ); } $ctx = array( 'form_id' => $form_id, 'schema' => Schema::get( $form_id ), 'values' => $values, 'entry' => $entry ); $status = in_array( $feed['post_status'], array( 'publish', 'draft', 'pending', 'private' ), true ) ? $feed['post_status'] : 'draft'; $author = 'fixed' === $feed['author'] ? (int) $feed['author_id'] : (int) ( $entry['user_id'] ?? 0 ); if ( ! $author && $feed['author_id'] ) { $author = (int) $feed['author_id']; } $title = trim( wp_strip_all_tags( Smartcodes::parse( (string) $feed['title'], $ctx, 'text' ) ) ); $post = array( 'post_type' => $pt, 'post_status' => $status, 'post_author' => $author, 'post_title' => '' !== $title ? $title : sprintf( /* translators: %d entry number */ __( 'Entry #%d', 'fieldwren' ), (int) ( $entry['serial'] ?? 0 ) ), 'post_content' => wp_kses_post( Smartcodes::parse( (string) $feed['content'], $ctx, 'html' ) ), 'post_excerpt' => sanitize_textarea_field( Smartcodes::parse( (string) $feed['excerpt'], $ctx, 'text' ) ), ); $match_key = sanitize_text_field( (string) $feed['update']['meta_key'] ); $match_val = $match_key ? trim( Smartcodes::parse( (string) $feed['update']['meta_value'], $ctx, 'text' ) ) : ''; if ( ! empty( $feed['update']['enabled'] ) && $match_key && '' !== $match_val ) { $found = get_posts( array( 'post_type' => $pt, 'post_status' => 'any', 'numberposts' => 1, 'fields' => 'ids', 'meta_key' => $match_key, // phpcs:ignore WordPress.DB.SlowDBQuery 'meta_value' => $match_val, // phpcs:ignore WordPress.DB.SlowDBQuery ) ); if ( $found ) { $post['ID'] = (int) $found[0]; unset( $post['post_author'] ); } } $post = apply_filters( 'fwren/post_creation/postarr', $post, $feed, $entry, $values ); $id = empty( $post['ID'] ) ? wp_insert_post( wp_slash( $post ), true ) : wp_update_post( wp_slash( $post ), true ); if ( is_wp_error( $id ) ) { return $id; } $id = (int) $id; if ( ! empty( $feed['update']['enabled'] ) && $match_key && '' !== $match_val ) { update_post_meta( $id, $match_key, wp_slash( $match_val ) ); } update_post_meta( $id, '_fwren_entry_id', (int) ( $entry['id'] ?? 0 ) ); update_post_meta( $id, '_fwren_form_id', (int) $form_id ); if ( $feed['featured_field'] ) { self::featured( $id, $values[ $feed['featured_field'] ] ?? array() ); } foreach ( (array) $feed['taxonomies'] as $t ) { self::terms( $id, $pt, (array) $t, $values, $ctx ); } foreach ( (array) $feed['meta'] as $m ) { self::meta( $id, (array) $m, $values, $ctx ); } return $id; } /** Featured image from an upload field value. */ private static function featured( $post_id, $files ) { $file = is_array( $files ) ? reset( $files ) : null; if ( ! is_array( $file ) ) { return; } if ( ! empty( $file['attachment_id'] ) && wp_attachment_is_image( (int) $file['attachment_id'] ) ) { set_post_thumbnail( $post_id, (int) $file['attachment_id'] ); return; } $path = (string) ( $file['path'] ?? '' ); if ( '' === $path || false !== strpos( $path, '..' ) ) { return; } $full = Install::upload_dir() . '/' . $path; $name = sanitize_file_name( (string) ( $file['name'] ?? basename( $path ) ) ); $ft = wp_check_filetype( $name ); if ( ! file_exists( $full ) || empty( $ft['type'] ) || 0 !== strpos( $ft['type'], 'image/' ) ) { return; } $bits = wp_upload_bits( $name, null, (string) file_get_contents( $full ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( ! empty( $bits['error'] ) ) { return; } $att = wp_insert_attachment( array( 'post_mime_type' => $ft['type'], 'post_title' => pathinfo( $name, PATHINFO_FILENAME ), 'post_status' => 'inherit', ), $bits['file'], $post_id ); if ( $att && ! is_wp_error( $att ) ) { require_once ABSPATH . 'wp-admin/includes/image.php'; wp_update_attachment_metadata( $att, wp_generate_attachment_metadata( $att, $bits['file'] ) ); set_post_thumbnail( $post_id, $att ); } } /** Terms from a field or a fixed list. */ private static function terms( $post_id, $pt, array $t, array $values, array $ctx ) { $tax = sanitize_key( $t['taxonomy'] ?? '' ); if ( ! $tax || ! taxonomy_exists( $tax ) || ! is_object_in_taxonomy( $pt, $tax ) ) { return; } if ( 'field' === ( $t['source'] ?? 'field' ) ) { $raw = $values[ $t['field'] ?? '' ] ?? array(); $raw = is_array( $raw ) ? $raw : preg_split( '/\s*,\s*/', (string) $raw ); } else { $raw = preg_split( '/\s*,\s*/', Smartcodes::parse( (string) ( $t['terms'] ?? '' ), $ctx, 'text' ) ); } $ids = array(); foreach ( (array) $raw as $r ) { if ( ! is_scalar( $r ) || '' === trim( (string) $r ) ) { continue; } $r = trim( (string) $r ); $term = is_numeric( $r ) ? get_term( (int) $r, $tax ) : null; if ( ! $term || is_wp_error( $term ) ) { $term = get_term_by( 'slug', sanitize_title( $r ), $tax ); } if ( ! $term ) { $term = get_term_by( 'name', $r, $tax ); } if ( $term && ! is_wp_error( $term ) ) { $ids[] = (int) $term->term_id; } elseif ( Helpers::on( $t['create'] ?? '' ) ) { $new = wp_insert_term( $r, $tax ); if ( ! is_wp_error( $new ) ) { $ids[] = (int) $new['term_id']; } } } if ( $ids ) { wp_set_object_terms( $post_id, array_values( array_unique( $ids ) ), $tax, false ); } } /** One meta row (post meta, ACF or Meta Box). */ private static function meta( $post_id, array $m, array $values, array $ctx ) { $key = sanitize_text_field( (string) ( $m['key'] ?? '' ) ); if ( '' === $key ) { return; } $tpl = trim( (string) ( $m['value'] ?? '' ) ); if ( preg_match( '/^\{inputs\.([a-z0-9_]+)\}$/i', $tpl, $mm ) && isset( $values[ $mm[1] ] ) && is_array( $values[ $mm[1] ] ) && ! self::is_files( $values[ $mm[1] ] ) ) { $value = $values[ $mm[1] ]; } else { $value = Smartcodes::parse( $tpl, $ctx, 'text' ); } $type = $m['type'] ?? 'meta'; if ( 'acf' === $type && function_exists( 'update_field' ) ) { update_field( $key, $value, $post_id ); } elseif ( 'metabox' === $type && function_exists( 'rwmb_set_meta' ) ) { rwmb_set_meta( $post_id, $key, $value ); } else { update_post_meta( $post_id, $key, wp_slash( $value ) ); } } /** @return bool Whether a value is a list of stored files. */ private static function is_files( array $v ) { $first = reset( $v ); return is_array( $first ) && isset( $first['path'] ); } /* ------------------------------------------------------------------ * * Admin * ------------------------------------------------------------------ */ /** Per-form tab. */ public static function tab( $tabs, $form_id = 0 ) { $tabs['post_feeds'] = __( 'Post feeds', 'fieldwren' ); return $tabs; } /** Tab content. */ public static function render_tab( $form_id, $settings ) { $feeds = self::feeds( $form_id ); $feeds[] = array_merge( self::blank(), array( '_new' => true, 'enabled' => false ) ); $types = array(); foreach ( get_post_types( array( 'show_ui' => true ), 'objects' ) as $pt ) { if ( ! in_array( $pt->name, array( 'attachment', Plugin::POST_TYPE, 'wp_block', 'wp_navigation', 'wp_template', 'wp_template_part' ), true ) ) { $types[ $pt->name ] = $pt->labels->singular_name; } } $taxes = array( '' => '—' ); foreach ( get_taxonomies( array( 'show_ui' => true ), 'objects' ) as $tx ) { $taxes[ $tx->name ] = $tx->labels->singular_name . ' (' . $tx->name . ')'; } $users = array( 0 => '—' ); foreach ( get_users( array( 'capability' => array( 'edit_posts' ), 'number' => 200, 'fields' => array( 'ID', 'display_name' ) ) ) as $u ) { $users[ $u->ID ] = $u->display_name; } $fields = array( '' => '—' ) + Schema::field_choices( $form_id ); $uploads = array( '' => '—' ) + Schema::field_choices( $form_id, Helpers::types( array( 'image', 'file' ) ) ); $mtypes = array( 'meta' => __( 'Post meta', 'fieldwren' ) ); if ( function_exists( 'update_field' ) ) { $mtypes['acf'] = 'ACF'; } if ( function_exists( 'rwmb_set_meta' ) ) { $mtypes['metabox'] = 'Meta Box'; } echo '

' . esc_html__( 'Each feed creates (or updates) a post from an entry, after double opt-in / approval / payment when those are on. Templates accept smartcodes such as {inputs.title}.', 'fieldwren' ) . '

'; foreach ( $feeds as $i => $feed ) { $feed = array_merge( self::blank(), (array) $feed ); $p = 'fwren_post_feeds[' . $i . ']'; $new = ! empty( $feed['_new'] ); echo '
1 ? '' : ' open' ) . '>' . esc_html( $new ? __( '+ Add a post feed', 'fieldwren' ) : ( $feed['name'] ? $feed['name'] : __( 'Post feed', 'fieldwren' ) ) ) . '' . ( ! $new && empty( $feed['enabled'] ) ? ' (' . esc_html__( 'off', 'fieldwren' ) . ')' : '' ) . ''; echo ''; echo ''; Helpers::row_text( $p . '[name]', $feed['name'], __( 'Feed name', 'fieldwren' ) ); Helpers::row_toggle( $p . '[enabled]', ! empty( $feed['enabled'] ) || $new, __( 'Enabled', 'fieldwren' ) ); Helpers::row_select( $p . '[post_type]', $feed['post_type'], __( 'Post type', 'fieldwren' ), $types ); Helpers::row_select( $p . '[post_status]', $feed['post_status'], __( 'Status', 'fieldwren' ), array( 'draft' => __( 'Draft', 'fieldwren' ), 'pending' => __( 'Pending review', 'fieldwren' ), 'publish' => __( 'Published', 'fieldwren' ), 'private' => __( 'Private', 'fieldwren' ) ) ); Helpers::row_select( $p . '[author]', $feed['author'], __( 'Author', 'fieldwren' ), array( 'current' => __( 'The logged-in submitter (else the fixed author)', 'fieldwren' ), 'fixed' => __( 'Fixed author', 'fieldwren' ) ) ); Helpers::row_select( $p . '[author_id]', (string) $feed['author_id'], __( 'Fixed author', 'fieldwren' ), $users ); Helpers::row_text( $p . '[title]', $feed['title'], __( 'Title', 'fieldwren' ) ); Helpers::row_textarea( $p . '[content]', $feed['content'], __( 'Content', 'fieldwren' ), __( 'HTML allowed.', 'fieldwren' ), 6 ); echo ''; Helpers::row_textarea( $p . '[excerpt]', $feed['excerpt'], __( 'Excerpt', 'fieldwren' ), '', 2 ); Helpers::row_select( $p . '[featured_field]', $feed['featured_field'], __( 'Featured image from', 'fieldwren' ), $uploads, __( 'An Image / File upload field; the first image becomes the featured image.', 'fieldwren' ) ); echo ''; echo ''; Helpers::row_conditions( $p . '[conditions]', $form_id, $feed['conditions'], __( 'Conditions', 'fieldwren' ) ); Helpers::row_toggle( $p . '[update][enabled]', ! empty( $feed['update']['enabled'] ), __( 'Update an existing post', 'fieldwren' ), __( 'When a post of this type has the meta key with the same value, it is updated instead of creating a new one. The value is saved on new posts too.', 'fieldwren' ) ); Helpers::row_text( $p . '[update][meta_key]', $feed['update']['meta_key'], __( 'Match meta key', 'fieldwren' ) ); Helpers::row_text( $p . '[update][meta_value]', $feed['update']['meta_value'], __( 'Match value', 'fieldwren' ), __( 'e.g. {inputs.email}', 'fieldwren' ) ); if ( ! $new ) { echo ''; // phpcs:ignore } echo '
'; } } /** Save feeds. */ public static function save_tab( $form_id, $post ) { if ( ! current_user_can( 'fwren_manage_forms' ) ) { return; } $in = isset( $post['fwren_post_feeds'] ) && is_array( $post['fwren_post_feeds'] ) ? wp_unslash( $post['fwren_post_feeds'] ) : array(); $out = array(); foreach ( $in as $f ) { if ( ! is_array( $f ) || Helpers::on( $f['_delete'] ?? '' ) ) { continue; } if ( '' === trim( (string) ( $f['id'] ?? '' ) ) && '' === trim( (string) ( $f['name'] ?? '' ) ) && '' === trim( (string) ( $f['title'] ?? '' ) ) && '' === trim( (string) ( $f['content'] ?? '' ) ) ) { continue; // Untouched "add" box. } $taxes = array(); foreach ( (array) ( $f['taxonomies'] ?? array() ) as $t ) { if ( is_array( $t ) && ! empty( $t['taxonomy'] ) ) { $taxes[] = array( 'taxonomy' => sanitize_key( $t['taxonomy'] ), 'source' => 'fixed' === ( $t['source'] ?? '' ) ? 'fixed' : 'field', 'field' => sanitize_key( $t['field'] ?? '' ), 'terms' => Helpers::text( $t['terms'] ?? '' ), 'create' => Helpers::on( $t['create'] ?? '' ), ); } } $meta = array(); foreach ( (array) ( $f['meta'] ?? array() ) as $m ) { if ( is_array( $m ) && '' !== trim( (string) ( $m['key'] ?? '' ) ) ) { $meta[] = array( 'key' => Helpers::text( $m['key'] ), 'value' => Helpers::text( $m['value'] ?? '' ), 'type' => in_array( $m['type'] ?? '', array( 'acf', 'metabox' ), true ) ? $m['type'] : 'meta', ); } } $out[] = array( 'id' => '' !== trim( (string) ( $f['id'] ?? '' ) ) ? sanitize_key( $f['id'] ) : 'pf' . substr( Helpers::token(), 0, 8 ), 'name' => Helpers::text( $f['name'] ?? '' ), 'enabled' => Helpers::on( $f['enabled'] ?? '' ), 'post_type' => sanitize_key( $f['post_type'] ?? 'post' ), 'post_status' => sanitize_key( $f['post_status'] ?? 'draft' ), 'author' => 'fixed' === ( $f['author'] ?? '' ) ? 'fixed' : 'current', 'author_id' => absint( $f['author_id'] ?? 0 ), 'title' => Helpers::text( $f['title'] ?? '' ), 'content' => Helpers::html( $f['content'] ?? '' ), 'excerpt' => sanitize_textarea_field( (string) ( $f['excerpt'] ?? '' ) ), 'featured_field' => sanitize_key( $f['featured_field'] ?? '' ), 'taxonomies' => $taxes, 'meta' => $meta, 'conditions' => Helpers::clean_conditions( $f['conditions'] ?? array() ), 'update' => array( 'enabled' => Helpers::on( $f['update']['enabled'] ?? '' ), 'meta_key' => Helpers::text( $f['update']['meta_key'] ?? '' ), // phpcs:ignore WordPress.DB.SlowDBQuery -- Settings array key, not a query. 'meta_value' => Helpers::text( $f['update']['meta_value'] ?? '' ), // phpcs:ignore WordPress.DB.SlowDBQuery -- Settings array key, not a query. ), ); } self::save_feeds( $form_id, $out ); } /** Created posts in the entry sidebar. */ public static function entry_sidebar( $entry ) { if ( empty( $entry['id'] ) ) { return; } $ids = array_unique( array_map( 'intval', wp_list_pluck( Entries::meta_rows( (int) $entry['id'], '_post_id' ), 'meta_value' ) ) ); $ids = array_filter( $ids, 'get_post' ); if ( ! $ids ) { return; } echo '

' . esc_html__( 'Created posts', 'fieldwren' ) . '

'; } }