@@ -17,9 +17,9 @@ class Feedback_Source {
1717 /**
1818 * The ID of the post or page that the feedback was created on.
1919 *
20- * @var int
20+ * @var string
2121 */
22- private $ id = 0 ;
22+ private $ id = '' ;
2323
2424 /**
2525 * The title of the post or page that the feedback was created on.
@@ -43,29 +43,62 @@ class Feedback_Source {
4343 */
4444 private $ page_number = 1 ;
4545
46+ /**
47+ * The source type of the feedback entry.
48+ * Possible values: single, widget, block_template, block_template_part
49+ *
50+ * @var string
51+ */
52+ private $ source_type = 'single ' ;
53+
54+ /**
55+ * The request URL of the feedback entry.
56+ *
57+ * @var string
58+ */
59+ private $ request_url = '' ;
60+
4661 /**
4762 * Constructor for Feedback_Source.
4863 *
49- * @param int $id The ID of the feedback entry.
50- * @param string $title The title of the feedback entry.
51- * @param int $page_number The page number of the feedback entry, default is 1.
64+ * @param string|int $id The Source ID = post ID, widget ID, block template ID, or 0 for homepage or non-post/page.
65+ * @param string $title The title of the feedback entry.
66+ * @param int $page_number The page number of the feedback entry, default is 1.
67+ * @param string $source_type The source type of the feedback entry, default is 'single'.
68+ * @param string $request_url The request URL of the feedback entry.
5269 */
53- public function __construct ( $ id , $ title , $ page_number = 1 ) {
70+ public function __construct ( $ id = 0 , $ title = '' , $ page_number = 1 , $ source_type = 'single ' , $ request_url = '' ) {
71+
72+ if ( is_numeric ( $ id ) ) {
73+ $ this ->id = $ id > 0 ? $ id : 0 ;
74+ } else {
75+ $ this ->id = $ id ;
76+ }
5477
55- $ this ->id = $ id > 0 ? (int ) $ id : 0 ;
5678 $ this ->title = $ title ;
5779 $ this ->page_number = $ page_number ;
58- $ this ->permalink = $ this ->id === 0 ? home_url () : '' ;
59-
60- if ( $ id <= 0 ) {
61- return ;
62- }
80+ $ this ->permalink = empty ( $ request_url ) ? home_url () : $ request_url ;
81+ $ this ->source_type = $ source_type ; // possible source types: single, widget, block_template, block_template_part
82+ $ this ->request_url = $ request_url ;
6383
64- $ entry_post = get_post ( $ id );
84+ if ( is_numeric ( $ id ) && ! empty ( $ id ) ) {
85+ $ entry_post = get_post ( (int ) $ id );
86+ if ( $ entry_post && $ entry_post ->post_status === 'publish ' ) {
87+ $ this ->permalink = get_permalink ( $ entry_post );
88+ $ this ->title = get_the_title ( $ entry_post );
89+ } elseif ( $ entry_post ) {
90+ $ this ->permalink = '' ;
6591
66- if ( $ entry_post && $ entry_post ->post_status === 'publish ' ) {
67- $ this ->permalink = get_permalink ( $ entry_post );
68- $ this ->title = get_the_title ( $ entry_post );
92+ if ( $ entry_post ->post_status === 'trash ' ) {
93+ /* translators: %s is the post title */
94+ $ this ->title = sprintf ( __ ( '(trashed) %s ' , 'jetpack-forms ' ), $ this ->title );
95+ }
96+ }
97+ if ( empty ( $ entry_post ) ) {
98+ /* translators: %s is the post title */
99+ $ this ->title = sprintf ( __ ( '(deleted) %s ' , 'jetpack-forms ' ), $ this ->title );
100+ $ this ->permalink = '' ;
101+ }
69102 }
70103 }
71104
@@ -83,11 +116,81 @@ public static function from_submission( $current_post, int $current_page_number
83116 return new self ( 0 , '' , $ current_page_number );
84117 }
85118
86- $ title = $ current_post ->post_title ?? '' ;
119+ $ title = $ current_post ->post_title ?? __ ( ' (no title) ' , ' jetpack-forms ' ) ;
87120
88121 return new self ( $ id , $ title , $ current_page_number );
89122 }
90123
124+ /**
125+ * Get the title of the current page. That we can then use to display in the feedback entry.
126+ *
127+ * @return string The title of the current page. That we want to show to the user. To tell them where the feedback was left.
128+ */
129+ private static function get_source_title () {
130+ if ( is_front_page () ) {
131+ return get_bloginfo ( 'name ' );
132+ }
133+ if ( is_home () ) {
134+ return get_the_title ( get_option ( 'page_for_posts ' , true ) );
135+ }
136+ if ( is_singular () ) {
137+ return get_the_title ();
138+ }
139+ if ( is_archive () ) {
140+ return get_the_archive_title ();
141+ }
142+ if ( is_search () ) {
143+ /* translators: %s is the search term */
144+ return sprintf ( __ ( 'Search results for: %s ' , 'jetpack-forms ' ), get_search_query () );
145+ }
146+ if ( is_404 () ) {
147+ return __ ( '404 Not Found ' , 'jetpack-forms ' );
148+ }
149+ return get_bloginfo ( 'name ' );
150+ }
151+
152+ /**
153+ * Creates a Feedback_Source instance for a block template.
154+ *
155+ * @param array $attributes Form Shortcode attributes.
156+ *
157+ * @return Feedback_Source Returns an instance of Feedback_Source.
158+ */
159+ public static function get_current ( $ attributes ) {
160+ global $ wp , $ page ;
161+ $ current_url = home_url ( add_query_arg ( array (), $ wp ->request ) );
162+ if ( isset ( $ attributes ['widget ' ] ) && ! empty ( $ attributes ['widget ' ] ) ) {
163+ return new self ( $ attributes ['widget ' ], self ::get_source_title (), 1 , 'widget ' , $ current_url );
164+ }
165+
166+ if ( isset ( $ attributes ['block_template ' ] ) && ! empty ( $ attributes ['block_template ' ] ) ) {
167+ global $ _wp_current_template_id ;
168+ return new self ( $ _wp_current_template_id , self ::get_source_title (), $ page , 'block_template ' , $ current_url );
169+ }
170+
171+ if ( isset ( $ attributes ['block_template_part ' ] ) && ! empty ( $ attributes ['block_template_part ' ] ) ) {
172+ return new self ( $ attributes ['block_template_part ' ], self ::get_source_title (), $ page , 'block_template_part ' , $ current_url );
173+ }
174+
175+ return new Feedback_Source ( \get_the_ID (), \get_the_title (), $ page , 'single ' , $ current_url );
176+ }
177+
178+ /**
179+ * Creates a Feedback_Source instance from serialized data.
180+ *
181+ * @param array $data The serialized data.
182+ * @return Feedback_Source Returns an instance of Feedback_Source.
183+ */
184+ public static function from_serialized ( $ data ) {
185+ $ id = $ data ['source_id ' ] ?? 0 ;
186+ $ title = $ data ['entry_title ' ] ?? '' ;
187+ $ page_number = $ data ['entry_page ' ] ?? 1 ;
188+ $ source_type = $ data ['source_type ' ] ?? 'single ' ;
189+ $ request_url = $ data ['request_url ' ] ?? '' ;
190+
191+ return new self ( $ id , $ title , $ page_number , $ source_type , $ request_url );
192+ }
193+
91194 /**
92195 * Get the permalink of the feedback entry.
93196 *
@@ -100,6 +203,38 @@ public function get_permalink() {
100203 return $ this ->permalink ;
101204 }
102205
206+ /**
207+ * Get the edit URL of the form or page where the feedback was submitted from.
208+ *
209+ * @return string The edit URL of the form or page.
210+ */
211+ public function get_edit_form_url () {
212+
213+ if ( current_user_can ( 'edit_theme_options ' ) ) {
214+ if ( $ this ->source_type === 'block_template ' && \wp_is_block_theme () ) {
215+ return admin_url ( 'site-editor.php?p= ' . esc_attr ( '/wp_template/ ' . addslashes ( $ this ->id ) ) . '&canvas=edit ' );
216+ }
217+
218+ if ( $ this ->source_type === 'block_template_part ' && \wp_is_block_theme () ) {
219+ return admin_url ( 'site-editor.php?p= ' . esc_attr ( '/wp_template_part/ ' . addslashes ( $ this ->id ) ) . '&canvas=edit ' );
220+ }
221+
222+ if ( $ this ->source_type === 'widget ' && current_theme_supports ( 'widgets ' ) ) {
223+ return admin_url ( 'widgets.php ' );
224+ }
225+ }
226+
227+ if ( $ this ->id && is_numeric ( $ this ->id ) && $ this ->id > 0 && current_user_can ( 'edit_post ' , (int ) $ this ->id ) ) {
228+ $ entry_post = get_post ( (int ) $ this ->id );
229+ if ( $ entry_post && $ entry_post ->post_status === 'trash ' ) {
230+ return '' ; // No edit link is possible for trashed posts. They need to be restored first.
231+ }
232+ return \get_edit_post_link ( (int ) $ this ->id , 'url ' );
233+ }
234+
235+ return '' ;
236+ }
237+
103238 /**
104239 * Get the relative permalink of the feedback entry.
105240 *
@@ -131,7 +266,7 @@ public function get_title() {
131266 /**
132267 * Get the post id of the feedback entry.
133268 *
134- * @return int The ID of the feedback entry.
269+ * @return int|string The ID of the feedback entry.
135270 */
136271 public function get_id () {
137272 return $ this ->id ;
@@ -146,6 +281,9 @@ public function serialize() {
146281 return array (
147282 'entry_title ' => $ this ->title ,
148283 'entry_page ' => $ this ->page_number ,
284+ 'source_id ' => $ this ->id ,
285+ 'source_type ' => $ this ->source_type ,
286+ 'request_url ' => $ this ->request_url ,
149287 );
150288 }
151289}
0 commit comments