|
2 | 2 | /* |
3 | 3 | * Populate prplSuggestedTasksTerms with the terms for the taxonomies we use. |
4 | 4 | * |
5 | | - * Dependencies: wp-api, progress-planner/document-ready |
| 5 | + * Dependencies: wp-api-fetch, progress-planner/document-ready |
6 | 6 | */ |
7 | 7 |
|
8 | 8 | const prplSuggestedTasksTerms = {}; |
@@ -40,48 +40,63 @@ const prplTerms = { |
40 | 40 | `Terms already fetched for taxonomy: ${ taxonomy }` |
41 | 41 | ); |
42 | 42 | resolve( prplSuggestedTasksTerms[ taxonomy ] ); |
| 43 | + return; |
43 | 44 | } |
44 | | - wp.api.loadPromise.done( () => { |
45 | | - console.info( `Fetching terms for taxonomy: ${ taxonomy }...` ); |
46 | 45 |
|
47 | | - const typeName = taxonomy.replace( 'prpl_', 'Prpl_' ); |
48 | | - prplSuggestedTasksTerms[ taxonomy ] = |
49 | | - prplSuggestedTasksTerms[ taxonomy ] || {}; |
50 | | - const TermsCollection = new wp.api.collections[ typeName ](); |
51 | | - TermsCollection.fetch( { data: { per_page: 100 } } ).done( |
52 | | - ( data ) => { |
53 | | - let userTermFound = false; |
54 | | - // 100 is the maximum number of terms that can be fetched in one request. |
55 | | - data.forEach( ( term ) => { |
56 | | - prplSuggestedTasksTerms[ taxonomy ][ term.slug ] = |
57 | | - term; |
58 | | - if ( 'user' === term.slug ) { |
59 | | - userTermFound = true; |
60 | | - } |
61 | | - } ); |
| 46 | + console.info( `Fetching terms for taxonomy: ${ taxonomy }...` ); |
62 | 47 |
|
63 | | - if ( userTermFound ) { |
64 | | - resolve( prplSuggestedTasksTerms[ taxonomy ] ); |
65 | | - } else { |
66 | | - // If the `user` term doesn't exist, create it. |
67 | | - const newTermModel = new wp.api.models[ typeName ]( |
68 | | - { |
69 | | - slug: 'user', |
70 | | - name: 'user', |
71 | | - } |
72 | | - ); |
73 | | - newTermModel |
74 | | - .save() |
75 | | - .then( ( response ) => { |
76 | | - prplSuggestedTasksTerms[ taxonomy ].user = |
77 | | - response; |
78 | | - return prplSuggestedTasksTerms[ taxonomy ]; |
79 | | - } ) |
80 | | - .then( resolve ); // Resolve the promise after all requests are complete. |
| 48 | + prplSuggestedTasksTerms[ taxonomy ] = |
| 49 | + prplSuggestedTasksTerms[ taxonomy ] || {}; |
| 50 | + |
| 51 | + // Fetch terms using wp.apiFetch. |
| 52 | + wp.apiFetch( { |
| 53 | + path: `/wp/v2/${ taxonomy }?per_page=100`, |
| 54 | + } ) |
| 55 | + .then( ( data ) => { |
| 56 | + let userTermFound = false; |
| 57 | + // 100 is the maximum number of terms that can be fetched in one request. |
| 58 | + data.forEach( ( term ) => { |
| 59 | + prplSuggestedTasksTerms[ taxonomy ][ term.slug ] = term; |
| 60 | + if ( 'user' === term.slug ) { |
| 61 | + userTermFound = true; |
81 | 62 | } |
| 63 | + } ); |
| 64 | + |
| 65 | + if ( userTermFound ) { |
| 66 | + resolve( prplSuggestedTasksTerms[ taxonomy ] ); |
| 67 | + } else { |
| 68 | + // If the `user` term doesn't exist, create it. |
| 69 | + wp.apiFetch( { |
| 70 | + path: `/wp/v2/${ taxonomy }`, |
| 71 | + method: 'POST', |
| 72 | + data: { |
| 73 | + slug: 'user', |
| 74 | + name: 'user', |
| 75 | + }, |
| 76 | + } ) |
| 77 | + .then( ( response ) => { |
| 78 | + prplSuggestedTasksTerms[ taxonomy ].user = |
| 79 | + response; |
| 80 | + resolve( prplSuggestedTasksTerms[ taxonomy ] ); |
| 81 | + } ) |
| 82 | + .catch( ( error ) => { |
| 83 | + console.error( |
| 84 | + `Error creating user term for taxonomy: ${ taxonomy }`, |
| 85 | + error |
| 86 | + ); |
| 87 | + // Resolve anyway even if creation fails. |
| 88 | + resolve( prplSuggestedTasksTerms[ taxonomy ] ); |
| 89 | + } ); |
82 | 90 | } |
83 | | - ); |
84 | | - } ); |
| 91 | + } ) |
| 92 | + .catch( ( error ) => { |
| 93 | + console.error( |
| 94 | + `Error fetching terms for taxonomy: ${ taxonomy }`, |
| 95 | + error |
| 96 | + ); |
| 97 | + // Resolve with empty object on error. |
| 98 | + resolve( prplSuggestedTasksTerms[ taxonomy ] || {} ); |
| 99 | + } ); |
85 | 100 | } ); |
86 | 101 | }, |
87 | 102 |
|
|
0 commit comments