Esempio n. 1
0
 setTimeout(() => {
   this.unsubscribe = subscribe(this.waitForSave);
 }, 0);
Esempio n. 2
0
		let wasSavingPost = select( 'core/editor' ).isSavingPost();
		let wasAutosavingPost = select( 'core/editor' ).isAutosavingPost();
		let wasPreviewingPost = select( 'core/editor' ).isPreviewingPost();
		// Save metaboxes when performing a full save on the post.
		subscribe( () => {
			const isSavingPost = select( 'core/editor' ).isSavingPost();
			const isAutosavingPost = select( 'core/editor' ).isAutosavingPost();
			const isPreviewingPost = select( 'core/editor' ).isPreviewingPost();
			const hasActiveMetaBoxes = select( 'core/edit-post' ).hasMetaBoxes();

			// Save metaboxes on save completion, except for autosaves that are not a post preview.
			const shouldTriggerMetaboxesSave = (
				hasActiveMetaBoxes && (
					( wasSavingPost && ! isSavingPost && ! wasAutosavingPost ) ||
					( wasAutosavingPost && wasPreviewingPost && ! isPreviewingPost )
				)
			);

			// Save current state for next inspection.
			wasSavingPost = isSavingPost;
			wasAutosavingPost = isAutosavingPost;
			wasPreviewingPost = isPreviewingPost;

			if ( shouldTriggerMetaboxesSave ) {
				store.dispatch( requestMetaBoxUpdates() );
			}
		} );
	},
	REQUEST_META_BOX_UPDATES( action, store ) {
		const state = store.getState();
Esempio n. 3
0
	/**
	 * Initializes analysis for the post edit screen.
	 *
	 * @returns {void}
	 */
	function initializePostAnalysis() {
		const editArgs = {
			onRefreshRequest: () => {},
			snippetEditorBaseUrl: wpseoPostScraperL10n.base_url,
			snippetEditorDate: wpseoPostScraperL10n.metaDescriptionDate,
			replaceVars: wpseoReplaceVarsL10n.replace_vars,
			recommendedReplaceVars: wpseoReplaceVarsL10n.recommended_replace_vars,
		};
		edit = new Edit( editArgs );

		editStore =  edit.getStore();
		const data = edit.getData();

		metaboxContainer = $( "#wpseo_meta" );

		tinyMCEHelper.setStore( editStore );
		tinyMCEHelper.wpTextViewOnInitCheck();
		handlePageBuilderCompatibility();

		// Avoid error when snippet metabox is not rendered.
		if ( metaboxContainer.length === 0 ) {
			return;
		}

		postDataCollector = initializePostDataCollector( data );
		publishBox.initialize();

		const appArgs = getAppArgs( editStore );
		app = new App( appArgs );

		edit.initializeAnnotations();

		// Expose globals.
		window.YoastSEO = {};
		window.YoastSEO.app = app;
		window.YoastSEO.store = editStore;
		window.YoastSEO.analysis = {};
		window.YoastSEO.analysis.worker = createAnalysisWorker();
		window.YoastSEO.analysis.collectData = () => collectAnalysisData( edit, YoastSEO.store, customAnalysisData, YoastSEO.app.pluggable );
		window.YoastSEO.analysis.applyMarks = ( paper, marks ) => getApplyMarks( YoastSEO.store )( paper, marks );

		// YoastSEO.app overwrites.
		YoastSEO.app.refresh = debounce( () => refreshAnalysis(
			YoastSEO.analysis.worker,
			YoastSEO.analysis.collectData,
			YoastSEO.analysis.applyMarks,
			YoastSEO.store,
			postDataCollector,
		), refreshDelay );
		YoastSEO.app.registerCustomDataCallback = customAnalysisData.register;
		YoastSEO.app.pluggable = new Pluggable( YoastSEO.app.refresh );
		YoastSEO.app.registerPlugin = YoastSEO.app.pluggable._registerPlugin;
		YoastSEO.app.pluginReady = YoastSEO.app.pluggable._ready;
		YoastSEO.app.pluginReloaded = YoastSEO.app.pluggable._reloaded;
		YoastSEO.app.registerModification = YoastSEO.app.pluggable._registerModification;
		YoastSEO.app.registerAssessment = ( name, assessment, pluginName ) => {
			if ( ! isUndefined( YoastSEO.app.seoAssessor ) ) {
				return YoastSEO.app.pluggable._registerAssessment( YoastSEO.app.defaultSeoAssessor, name, assessment, pluginName ) &&
					YoastSEO.app.pluggable._registerAssessment( YoastSEO.app.cornerStoneSeoAssessor, name, assessment, pluginName );
			}
		};
		YoastSEO.app.changeAssessorOptions = function( assessorOptions ) {
			YoastSEO.analysis.worker.initialize( assessorOptions ).catch( handleWorkerError );
			YoastSEO.app.refresh();
		};

		edit.initializeUsedKeywords( YoastSEO.app.refresh, "get_focus_keyword_usage" );

		editStore.subscribe( handleStoreChange.bind( null, editStore, YoastSEO.app.refresh ) );

		const replaceVarsPlugin = new YoastReplaceVarPlugin( app, editStore );
		const shortcodePlugin = new YoastShortcodePlugin( {
			registerPlugin: YoastSEO.app.registerPlugin,
			registerModification: YoastSEO.app.registerModification,
			pluginReady: YoastSEO.app.pluginReady,
			pluginReloaded: YoastSEO.app.pluginReloaded,
		} );

		if ( wpseoPostScraperL10n.markdownEnabled ) {
			const markdownPlugin = new YoastMarkdownPlugin( YoastSEO.app.registerPlugin, YoastSEO.app.registerModification );
			markdownPlugin.register();
		}

		exposeGlobals( replaceVarsPlugin, shortcodePlugin );

		activateEnabledAnalysis();

		YoastSEO._registerReactComponent = registerReactComponent;

		// Initialize the analysis worker.
		YoastSEO.analysis.worker.initialize( getAnalysisConfiguration() )
			.then( () => {
				jQuery( window ).trigger( "YoastSEO:ready" );
			} )
			.catch( handleWorkerError );

		// Backwards compatibility.
		YoastSEO.analyzerArgs = appArgs;

		postDataCollector.bindElementEvents( debounce( () => refreshAnalysis(
			YoastSEO.analysis.worker,
			YoastSEO.analysis.collectData,
			YoastSEO.analysis.applyMarks,
			YoastSEO.store,
			postDataCollector,
		), refreshDelay ) );

		// Hack needed to make sure Publish box and traffic light are still updated.
		disableYoastSEORenderers( app );
		const originalInitAssessorPresenters = app.initAssessorPresenters.bind( app );
		app.initAssessorPresenters = function() {
			originalInitAssessorPresenters();
			disableYoastSEORenderers( app );
		};

		// Set refresh function. data.setRefresh is only defined when Gutenberg is available.
		if ( data.setRefresh ) {
			data.setRefresh( app.refresh );
		}

		// Initialize the snippet editor data.
		let snippetEditorData = snippetEditorHelpers.getDataFromCollector( postDataCollector );
		const snippetEditorTemplates = snippetEditorHelpers.getTemplatesFromL10n( wpseoPostScraperL10n );
		snippetEditorData = snippetEditorHelpers.getDataWithTemplates( snippetEditorData, snippetEditorTemplates );

		// Set the initial snippet editor data.
		editStore.dispatch( updateData( snippetEditorData ) );
		// This used to be a checkbox, then became a hidden input. For consistency, we set the value to '1'.
		editStore.dispatch( setCornerstoneContent( document.getElementById( "yoast_wpseo_is_cornerstone" ).value === "1" ) );

		// Save the keyword, in order to compare it to store changes.
		let focusKeyword = editStore.getState().focusKeyword;
		requestWordsToHighlight( YoastSEO.analysis.worker.runResearch, YoastSEO.store, focusKeyword );
		const refreshAfterFocusKeywordChange = debounce( () => {
			app.refresh();
		}, 50 );

		let previousCornerstoneValue = null;
		editStore.subscribe( () => {
			// Verify whether the focusKeyword changed. If so, trigger refresh:
			const newFocusKeyword = editStore.getState().focusKeyword;

			if ( focusKeyword !== newFocusKeyword ) {
				focusKeyword = newFocusKeyword;
				requestWordsToHighlight( YoastSEO.analysis.worker.runResearch, YoastSEO.store, focusKeyword );

				$( "#yoast_wpseo_focuskw" ).val( focusKeyword );
				refreshAfterFocusKeywordChange();
			}

			const data = snippetEditorHelpers.getDataFromStore( editStore );
			const dataWithoutTemplates = snippetEditorHelpers.getDataWithoutTemplates( data, snippetEditorTemplates );


			if ( snippetEditorData.title !== data.title ) {
				postDataCollector.setDataFromSnippet( dataWithoutTemplates.title, "snippet_title" );
			}

			if ( snippetEditorData.slug !== data.slug ) {
				postDataCollector.setDataFromSnippet( dataWithoutTemplates.slug, "snippet_cite" );
			}

			if ( snippetEditorData.description !== data.description ) {
				postDataCollector.setDataFromSnippet( dataWithoutTemplates.description, "snippet_meta" );
			}

			const currentState = editStore.getState();

			if ( previousCornerstoneValue !== currentState.isCornerstone ) {
				previousCornerstoneValue = currentState.isCornerstone;
				document.getElementById( "yoast_wpseo_is_cornerstone" ).value = currentState.isCornerstone;

				app.changeAssessorOptions( {
					useCornerstone: currentState.isCornerstone,
				} );
			}

			snippetEditorData.title = data.title;
			snippetEditorData.slug = data.slug;
			snippetEditorData.description = data.description;
		} );

		if ( isGutenbergDataAvailable() ) {
			let editorMode = getEditorMode();

			toggleMarkers( editorMode, editStore );

			subscribe( () => {
				const currentEditorMode = getEditorMode();

				if ( currentEditorMode === editorMode ) {
					return;
				}

				editorMode = currentEditorMode;
				toggleMarkers( editorMode, editStore );
			} );
		}

		if ( ! isGutenbergDataAvailable() ) {
			renderClassicEditorMetabox( editStore );
		}

		initializationDone();
		YoastSEO.app.refresh();
	}
Esempio n. 4
0
			}
			return memo;
		}, {} );
		store.dispatch( setMetaBoxSavedData( dataPerLocation ) );

		let wasSavingPost = select( 'core/editor' ).isSavingPost();
		let wasAutosavingPost = select( 'core/editor' ).isAutosavingPost();
		// Save metaboxes when performing a full save on the post.
		subscribe( () => {
			const isSavingPost = select( 'core/editor' ).isSavingPost();
			const isAutosavingPost = select( 'core/editor' ).isAutosavingPost();

			// Save metaboxes on save completion when past save wasn't an autosave.
			const shouldTriggerMetaboxesSave = wasSavingPost && ! wasAutosavingPost && ! isSavingPost && ! isAutosavingPost;

			// Save current state for next inspection.
			wasSavingPost = isSavingPost;
			wasAutosavingPost = isAutosavingPost;

			if ( shouldTriggerMetaboxesSave ) {
				store.dispatch( requestMetaBoxUpdates() );
			}
		} );
	},
	REQUEST_META_BOX_UPDATES( action, store ) {
		const state = store.getState();
		const dataPerLocation = reduce( getMetaBoxes( state ), ( memo, metabox, location ) => {
			if ( metabox.isActive ) {
				memo[ location ] = jQuery( getMetaBoxContainer( location ) ).serialize();
			}
			return memo;
Esempio n. 5
0
		// Initialize metaboxes state
		const dataPerLocation = reduce( action.metaBoxes, ( memo, isActive, location ) => {
			if ( isActive ) {
				memo[ location ] = jQuery( getMetaBoxContainer( location ) ).serialize();
			}
			return memo;
		}, {} );
		store.dispatch( setMetaBoxSavedData( dataPerLocation ) );

		// Saving metaboxes when saving posts
		let previousIsSaving = select( 'core/editor' ).isSavingPost();
		subscribe( () => {
			const isSavingPost = select( 'core/editor' ).isSavingPost();
			const shouldTriggerSaving = ! isSavingPost && previousIsSaving;
			previousIsSaving = isSavingPost;
			if ( shouldTriggerSaving ) {
				store.dispatch( requestMetaBoxUpdates() );
			}
		} );
	},
	REQUEST_META_BOX_UPDATES( action, store ) {
		const state = store.getState();
		const dataPerLocation = reduce( getMetaBoxes( state ), ( memo, metabox, location ) => {
			if ( metabox.isActive ) {
				memo[ location ] = jQuery( getMetaBoxContainer( location ) ).serialize();
			}
			return memo;
		}, {} );
		store.dispatch( setMetaBoxSavedData( dataPerLocation ) );

		// Additional data needed for backwards compatibility.