Repository navigation
Forms: add forms list to dashboard #46580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9538bf5
9b9b173
d5d13a7
f698959
1456547
4c81129
9442f85
1bb0ade
4251785
bf9f2dd
666a994
417d58a
b159924
fc4335a
2f00675
2a016dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: minor | ||
| Type: added | ||
|
|
||
| Forms: add reusable forms list to dashboard. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,17 +7,144 @@ | |
|
|
||
| namespace Automattic\Jetpack\Forms\ContactForm; | ||
|
|
||
| use WP_REST_Request; | ||
|
|
||
| /** | ||
| * REST endpoint for the jetpack_form custom post type. | ||
| */ | ||
| class Jetpack_Form_Endpoint extends \WP_REST_Posts_Controller { | ||
| /** | ||
| * Cached map of form_id => entries count for the current request. | ||
| * | ||
| * @var array<int,int>|null | ||
| */ | ||
| private $entries_count_by_form_id = null; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| */ | ||
| public function __construct() { | ||
| parent::__construct( Contact_Form::POST_TYPE ); | ||
| } | ||
|
|
||
| /** | ||
| * Add opt-in dashboard fields. | ||
| * | ||
| * @return array | ||
| */ | ||
| public function get_collection_params() { | ||
| $params = parent::get_collection_params(); | ||
|
|
||
| // Note: We do not use the built-in WP REST "context" param for this, because it's validated | ||
| // against core values (view/embed/edit). This param is for Jetpack Forms dashboard usage only. | ||
| $params['jetpack_forms_context'] = array( | ||
| 'description' => __( 'Request context for Jetpack Forms. Use "dashboard" to include dashboard-only fields.', 'jetpack-forms' ), | ||
| 'type' => 'string', | ||
| 'default' => '', | ||
| 'enum' => array( '', 'dashboard' ), | ||
| 'sanitize_callback' => 'sanitize_key', | ||
| ); | ||
|
|
||
| return $params; | ||
| } | ||
|
|
||
| /** | ||
| * Return a collection of forms. | ||
| * | ||
| * We override this to compute dashboard aggregate fields in a single pass. | ||
| * | ||
| * @param WP_REST_Request $request Full details about the request. | ||
| * @return \WP_REST_Response|\WP_Error | ||
| */ | ||
| public function get_items( $request ) { | ||
| $response = parent::get_items( $request ); | ||
|
|
||
| if ( is_wp_error( $response ) ) { | ||
| return $response; | ||
| } | ||
|
|
||
| $forms_context = (string) $request->get_param( 'jetpack_forms_context' ); | ||
| if ( 'dashboard' !== $forms_context ) { | ||
| return $response; | ||
| } | ||
|
|
||
| $forms = $response->get_data(); | ||
| if ( ! is_array( $forms ) || empty( $forms ) ) { | ||
| return $response; | ||
| } | ||
|
|
||
| $form_ids = array(); | ||
| foreach ( $forms as $form ) { | ||
| if ( isset( $form['id'] ) ) { | ||
| $form_ids[] = (int) $form['id']; | ||
| } | ||
| } | ||
| $form_ids = array_values( array_unique( array_filter( $form_ids ) ) ); | ||
|
|
||
| $this->entries_count_by_form_id = $this->get_entries_count_by_form_id( $form_ids ); | ||
|
|
||
| foreach ( $forms as &$form ) { | ||
| $form_id = isset( $form['id'] ) ? (int) $form['id'] : 0; | ||
| $form['entries_count'] = (int) ( $this->entries_count_by_form_id[ $form_id ] ?? 0 ); | ||
| if ( $form_id ) { | ||
| $form['edit_url'] = get_edit_post_link( $form_id, 'raw' ); | ||
| } | ||
| } | ||
|
|
||
| $response->set_data( $forms ); | ||
| return $response; | ||
| } | ||
|
|
||
| /** | ||
| * Batch compute feedback counts for a list of form IDs. | ||
| * | ||
| * @param int[] $form_ids Form IDs to count entries for. | ||
| * @return array<int,int> Map of form_id => count | ||
| */ | ||
| private function get_entries_count_by_form_id( array $form_ids ): array { | ||
| global $wpdb; | ||
|
|
||
| $form_ids = array_values( array_unique( array_map( 'absint', $form_ids ) ) ); | ||
| if ( empty( $form_ids ) ) { | ||
| return array(); | ||
| } | ||
|
|
||
| // Count only "inbox-visible" feedback statuses. | ||
| // Note: This is about feedback (response) statuses, not form post statuses (publish/draft/pending/future/private). | ||
| $statuses = array( 'publish', 'draft' ); | ||
|
edanzer marked this conversation as resolved.
Resolved
edanzer marked this conversation as resolved.
Resolved
|
||
|
|
||
| // Cache the grouped counts briefly to avoid repeated DB hits (e.g. on reload / concurrent requests). | ||
| sort( $form_ids ); | ||
| $cache_key = 'feedback_counts_' . md5( implode( ',', $form_ids ) . '|' . implode( ',', $statuses ) ); | ||
| $cache_group = 'jetpack_forms'; | ||
| $cached = wp_cache_get( $cache_key, $cache_group ); | ||
| if ( false !== $cached && is_array( $cached ) ) { | ||
| return $cached; | ||
| } | ||
|
|
||
| $args = array_merge( array( Feedback::POST_TYPE ), $form_ids, $statuses ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like this is kind of hard to read and follow.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree hard to read. But I don't think we can do the change you're suggesting - it would change the data structure from flat to nested arrays. That said, this is not my strong suite, and I'm getting AI help with this. After some back and forth, I had my AI try to summarize the issue. You can let me know if you know better :).
So if $args is: then from prepare()’s perspective you supplied 3 replacement values total:
But the SQL has many placeholders:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the explanation Erick. I think what you have works well. One thing that I think would help here is still remobing the $args variable. instead.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @enej - I think that's still the same thing and presents the same concerns? |
||
|
|
||
| // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching | ||
| $rows = $wpdb->get_results( | ||
| $wpdb->prepare( | ||
| "SELECT post_parent, COUNT(1) AS entry_count | ||
| FROM {$wpdb->posts} | ||
| WHERE post_type = %s | ||
| AND post_parent IN (" . implode( ',', array_fill( 0, count( $form_ids ), '%d' ) ) . ') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if that is the best way to do this. I think it can be simplified a lot. by just passing in a string in here instead.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey - Needed to think this through more. I see what you're asking. Again running SQL queries is not my strong suit, so lots of back and forth with AI to try to understand what's needed here. My current understanding is that prepare() only safely substitutes values into placeholders (%d, %s, etc.). So we could just add as string there, and the query will likely run, but prepare() isn’t protecting that part, PHPCS will complain, and (maybe) it is easier to introduce bugs/injection. That said, if you want to go straight string route, I can try to update that. |
||
| AND post_status IN (' . implode( ',', array_fill( 0, count( $statuses ), '%s' ) ) . ') | ||
| GROUP BY post_parent', | ||
| $args | ||
| ) | ||
| ); | ||
|
edanzer marked this conversation as resolved.
Resolved
|
||
| $counts_by_form_id = array(); | ||
| foreach ( (array) $rows as $row ) { | ||
| $counts_by_form_id[ (int) $row->post_parent ] = (int) $row->entry_count; | ||
| } | ||
|
|
||
| wp_cache_set( $cache_key, $counts_by_form_id, $cache_group, 15 ); // 15 seconds. | ||
| return $counts_by_form_id; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a given request has access to get items. | ||
| * | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.