public function formSubmit() {

		if ( empty( $this->isFormSubmission() ) || empty( current_user_can( 'edit_others_posts' ) ) ) {
			// Don't do anything in this function, bail.
			return;
		}

		// Process form
		if ( isset( $_GET['custom_styles'] ) && $_GET['custom_styles'] === 'yes' && isset( $_POST['your_styles'] ) && check_admin_referer( 'pb-custom-styles' ) ) {
			$slug = isset( $_POST['slug'] ) ? $_POST['slug'] : 'web';
			$redirect_url = get_admin_url( get_current_blog_id(), '/themes.php?page=' . $this::PAGE . '&slug=' . $slug );

			if ( ! isset( $_POST['post_id'], $_POST['post_id_integrity'] ) ) {
				debug_error_log( __METHOD__ . ' error: Missing post ID' );
				\Pressbooks\Redirect\location( $redirect_url . '&custom_styles_error=true' );
			}
			if ( md5( NONCE_KEY . $_POST['post_id'] ) !== $_POST['post_id_integrity'] ) {
				// A hacker trying to overwrite posts?.
				debug_error_log( __METHOD__ . ' error: unexpected value for post_id_integrity' );
				\Pressbooks\Redirect\location( $redirect_url . '&custom_styles_error=true' );
			}

			// Remove wp_filter_post_kses, this causes CSS escaping issues
			remove_filter( 'content_save_pre', 'wp_filter_post_kses' );
			remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' );
			remove_all_filters( 'content_save_pre' );

			// Write to database
			$my_post = [
				'ID' => absint( $_POST['post_id'] ),
				'post_content' => \Pressbooks\Sanitize\cleanup_css( $_POST['your_styles'] ),
			];
			$response = wp_update_post( $my_post, true );

			if ( is_wp_error( $response ) ) {
				// Something went wrong?
				debug_error_log( __METHOD__ . ' error, wp_update_post(): ' . $response->get_error_message() );
				\Pressbooks\Redirect\location( $redirect_url . '&custom_styles_error=true' );
			}

			if ( $slug === 'web' ) {
				// a recompile will be triggered whenever the user saves custom styles targeting web
				$this->updateWebBookStyleSheet();
			}

			// Ok!
			\Pressbooks\Redirect\location( $redirect_url );
		}

	}
public function formSubmit() {

		if ( empty( $this->isFormSubmission() ) || empty( current_user_can( 'edit_others_posts' ) ) ) {
			// Don't do anything in this function, bail.
			return;
		}

		// Process form
		if ( isset( $_GET['custom_styles'] ) && $_GET['custom_styles'] === 'yes' && isset( $_POST['your_styles'] ) && check_admin_referer( 'pb-custom-styles' ) ) {
			$slug = isset( $_POST['slug'] ) ? $_POST['slug'] : 'web';
			$redirect_url = get_admin_url( get_current_blog_id(), '/themes.php?page=' . $this::PAGE . '&slug=' . $slug );

			if ( ! isset( $_POST['post_id'], $_POST['post_id_integrity'] ) ) {
				debug_error_log( __METHOD__ . ' error: Missing post ID' );
				\Pressbooks\Redirect\location( $redirect_url . '&custom_styles_error=true' );
			}
			if ( md5( NONCE_KEY . $_POST['post_id'] ) !== $_POST['post_id_integrity'] ) {
				// A hacker trying to overwrite posts?.
				debug_error_log( __METHOD__ . ' error: unexpected value for post_id_integrity' );
				\Pressbooks\Redirect\location( $redirect_url . '&custom_styles_error=true' );
			}

			// Remove wp_filter_post_kses, this causes CSS escaping issues
			remove_filter( 'content_save_pre', 'wp_filter_post_kses' );
			remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' );
			remove_all_filters( 'content_save_pre' );

			// Write to database
			$my_post = [
				'ID' => absint( $_POST['post_id'] ),
				'post_content' => \Pressbooks\Sanitize\cleanup_css( $_POST['your_styles'] ),
			];
			$response = wp_update_post( $my_post, true );

			if ( is_wp_error( $response ) ) {
				// Something went wrong?
				debug_error_log( __METHOD__ . ' error, wp_update_post(): ' . $response->get_error_message() );
				\Pressbooks\Redirect\location( $redirect_url . '&custom_styles_error=true' );
			}

			if ( $slug === 'web' ) {
				// a recompile will be triggered whenever the user saves custom styles targeting web
				$this->updateWebBookStyleSheet();
			}

			// Ok!
			\Pressbooks\Redirect\location( $redirect_url );
		}

	}
public function formSubmit ( ) { if ( empty ( $ this -> isFormSubmission ( ) ) || empty ( current_user_can ( 'edit_others_posts' ) ) ) { // Don't do anything in this function, bail. return ; } // Process form if ( isset ( $ _GET [ 'custom_styles' ] ) && $ _GET [ 'custom_styles' ] === 'yes' && isset ( $ _POST [ 'your_styles' ] ) && check_admin_referer ( 'pb-custom-styles' ) ) { $ slug = isset ( $ _POST [ 'slug' ] ) ? $ _POST [ 'slug' ] : 'web' ; $ redirect_url = get_admin_url ( get_current_blog_id ( ) , '/themes.php?page=' . $ this :: PAGE . '&slug=' . $ slug ) ; if ( ! isset ( $ _POST [ 'post_id' ] , $ _POST [ 'post_id_integrity' ] ) ) { debug_error_log ( __METHOD__ . ' error: Missing post ID' ) ; \ Pressbooks \ Redirect \ location ( $ redirect_url . '&custom_styles_error=true' ) ; } if ( md5 ( NONCE_KEY . $ _POST [ 'post_id' ] ) !== $ _POST [ 'post_id_integrity' ] ) { // A hacker trying to overwrite posts?. debug_error_log ( __METHOD__ . ' error: unexpected value for post_id_integrity' ) ; \ Pressbooks \ Redirect \ location ( $ redirect_url . '&custom_styles_error=true' ) ; } // Remove wp_filter_post_kses, this causes CSS escaping issues remove_filter ( 'content_save_pre' , 'wp_filter_post_kses' ) ; remove_filter ( 'content_filtered_save_pre' , 'wp_filter_post_kses' ) ; remove_all_filters ( 'content_save_pre' ) ; // Write to database $ my_post = [ 'ID' => absint ( $ _POST [ 'post_id' ] ) , 'post_content' => \ Pressbooks \ Sanitize \ cleanup_css ( $ _POST [ 'your_styles' ] ) , ] ; $ response = wp_update_post ( $ my_post , true ) ; if ( is_wp_error ( $ response ) ) { // Something went wrong? debug_error_log ( __METHOD__ . ' error, wp_update_post(): ' . $ response -> get_error_message ( ) ) ; \ Pressbooks \ Redirect \ location ( $ redirect_url . '&custom_styles_error=true' ) ; } if ( $ slug === 'web' ) { // a recompile will be triggered whenever the user saves custom styles targeting web $ this -> updateWebBookStyleSheet ( ) ; } // Ok! \ Pressbooks \ Redirect \ location ( $ redirect_url ) ; } }
Save custom styles to database
Save custom styles to database