PluginProbe
Parse.ly / 3.20.6
Parse.ly v3.20.6
3.24.2 3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 All 106 releases
wp-parsely / src / content-helper / common / utils / functions.ts

functions.ts in Parse.ly 3.20.6, at src/content-helper/common/utils/functions.ts

114 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { select, subscribe } from '@wordpress/data';
2
3 /**
4 * Escapes special characters in a string for use in a regular expression.
5 *
6 * @since 3.14.0
7 * @since 3.14.1 Moved from `editor-sidebar/smart-linking/utils.ts` to `common/utils/functions.ts`.
8 *
9 * @param {string} string The string to be escaped.
10 *
11 * @return {string} The escaped string.
12 */
13 export function escapeRegExp( string: string ): string {
14 return string.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ); // $& means the whole matched string.
15 }
16
17 /**
18 * Generates both http and https versions of a URL.
19 *
20 * @since 3.14.3
21 *
22 * @param {string} url The URL to generate protocol variants for.
23 *
24 * @return {string[]} An array containing both http and https versions of the URL.
25 */
26 export function generateProtocolVariants( url: string ): string[] {
27 const strippedUrl = url.replace( /^https?:\/\//i, '' );
28
29 const httpUrl = 'http://' + strippedUrl;
30 const httpsUrl = 'https://' + strippedUrl;
31
32 return [ httpUrl, httpsUrl ];
33 }
34
35 /**
36 * Checks if the editor is ready to be interacted with.
37 * It waits for the editor to be clean or to have at least one block, and it resolves when it's ready.
38 *
39 * @since 3.14.0
40 * @since 3.16.0 Moved from `editor-sidebar/smart-linking/component-block-change.tsx`
41 * to `common/utils/functions.ts`.
42 */
43 export async function isEditorReady(): Promise<void> {
44 return new Promise( ( resolve ) => {
45 const unsubscribeEditorReady = subscribe( () => {
46 if ( select( 'core/editor' ).isCleanNewPost() || select( 'core/block-editor' ).getBlockCount() > 0 ) {
47 unsubscribeEditorReady();
48 resolve();
49 }
50 } );
51 } );
52 }
53
54 /**
55 * Adds ITM parameters to a URL.
56 *
57 * @since 3.19.0
58 *
59 * @param {string} url The URL to add ITM parameters to.
60 * @param {Object} itmParams The ITM parameters to add to the URL.
61 * @param {string} itmParams.campaign The campaign parameter.
62 * @param {string} itmParams.source The source parameter.
63 * @param {string} itmParams.medium The medium parameter.
64 * @param {string} itmParams.content The content parameter.
65 * @param {string} itmParams.term The term parameter.
66 *
67 * @return {string} The URL with ITM parameters added.
68 */
69 export function addITMParamsToURL( url: string, itmParams: {
70 campaign: string;
71 source?: string;
72 medium?: string;
73 content?: string;
74 term?: string;
75 } ): string {
76 const urlObj = new URL( url );
77 urlObj.searchParams.set( 'itm_campaign', itmParams.campaign );
78
79 if ( itmParams.source ) {
80 urlObj.searchParams.set( 'itm_source', itmParams.source );
81 }
82 if ( itmParams.medium ) {
83 urlObj.searchParams.set( 'itm_medium', itmParams.medium );
84 }
85 if ( itmParams.content ) {
86 urlObj.searchParams.set( 'itm_content', itmParams.content );
87 }
88 if ( itmParams.term ) {
89 urlObj.searchParams.set( 'itm_term', itmParams.term );
90 }
91
92 return urlObj.toString();
93 }
94
95 /**
96 * Removes ITM parameters from a URL.
97 *
98 * @since 3.19.0
99 *
100 * @param {string} url The URL to remove ITM parameters from.
101 *
102 * @return {string} The URL with ITM parameters removed.
103 */
104 export function removeITMParamsFromURL( url: string ): string {
105 const urlObj = new URL( url );
106 urlObj.searchParams.delete( 'itm_campaign' );
107 urlObj.searchParams.delete( 'itm_source' );
108 urlObj.searchParams.delete( 'itm_medium' );
109 urlObj.searchParams.delete( 'itm_content' );
110 urlObj.searchParams.delete( 'itm_term' );
111
112 return urlObj.toString();
113 }
114