'', '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 '| '; Helpers::smartcodes_button( Helpers::dom_id( $p . '[content]' ), $form_id ); echo ' | |||||||||||
| ' . esc_html__( 'Taxonomies', 'fieldwren' ) . ' |
| ||||||||||
| ' . esc_html__( 'Custom fields', 'fieldwren' ) . ' |
' . esc_html__( 'A value that is exactly {inputs.key} of a multi-value field is saved as an array.', 'fieldwren' ) . ' | ||||||||||
| ' . Helpers::checkbox( $p . '[_delete]', false, __( 'Delete this feed', 'fieldwren' ) ) . ' |