Forums Forums Search Search Results for 'save_post add_action'

Viewing 6 results - 11 through 16 (of 16 total)
  • Author
    Search Results
  • #146253

    Anonymous
    Inactive

    Hi Trevor, thanks for pointing me to here, however the fix did not work. I am not sure if I have missed something though, all I have done here is add

    function my_acf_save_post( $post_id ) {
        
    	// bail early if no ACF data
    	if( empty($_POST['acf']) ) {
    		return;        
    	}
    	
    	if ((!is_admin()) || (defined('DOING_AJAX') && DOING_AJAX)) {
    		do_action("search_filter_update_post_cache", $post_id);
    	}    
    }
    
    add_action('acf/save_post', 'my_acf_save_post', 20);

    to my functions file… I have tried to backtrack through this thread to see if something is missing. There are many private messages though so it’s difficult to tell.

    #131069

    Ross
    Keymaster

    Hi Jon-Sun Lu

    Sorry for the delay on this.

    So yes, you’ve run into a limitation with S&F currently.

    Essentially, the meta key of your field, is automatically used as the name in the URL.

    So, when this getts added to the URL

    _wpbdp[fields][22]

    it would be

    _sfm__wpbdp[fields][22]

    However, even in URLs, this signifies that the $_GET var is an array called _sfm__wpbdp

    This means its not straight forward to get the value – and is not currently supported.

    We’re working on V3 of the plugin, which allows you to map the internal meta name, and create your own URL arg, but that is a while away yet.

    The only 2 options I can see are as follows:

    1) See if you can store the data from the directly plugin, in a different way, or using your own meta keys, if not, you would need to use a custom field plugin (such as ACF, PODs, or built in WP Custom field option)

    2) IF you do not plan to add tons of fields, and its only this one you are having difficulty with, what you could do is create a bit of custom code, which copies this meta value from _wpbdp[fields][22] into another custom field, with a name you choose (that doesn’t look like an array).

    I wrote a little scrip to help you get started, its untested but looks correct to me:

    //whenever a post is saved, copy the meta value, if it exists
    add_action( 'save_post', 'copy_meta_data', 100, 1 );
    function copy_meta_data($post_id)
    {
    	//remove action once its init
    	remove_action( 'save_post', 'copy_meta_data', 100, 1 );
    	
    	//grab the existing value from the DB
    	$existing_value = get_post_meta($post_id, '_wpbdp[fields][22]', true);
    	
    	//make sure it exists
    	if ( ! empty( $existing_value ) ) {
    		//now add it to a new meta key, with a name of our choosing
    		$new_meta_key = 'my_new_meta_key';
    		update_post_meta($post_id, $new_meta_key, $existing_value);
    	}
    }

    With the example above, you would then add a field to your search form, with the meta key my_new_meta_key.

    Let me know how you get on.

    Thanks

    #94346

    Ross
    Keymaster

    Ok great news, problem solved (well, we have a workaround)!

    I’ve not really used frontend submissions so took a little more time to familiarise myself with it.

    I found the acf action / equivalent for save_post which is acf/save_post and looks like (with the correct priority) we can get the save_post action to return the correct meta data.

    https://www.advancedcustomfields.com/resources/acf-save_post/

    So, my solution is to trigger cache update manually when acf/save_post.

    Add this to the bottom of functions.php:

    function my_acf_save_post( $post_id ) {
        
    	// bail early if no ACF data
    	if( empty($_POST['acf']) ) {
    		return;        
    	}
    	
    	if ((!is_admin()) || (defined('DOING_AJAX') && DOING_AJAX)) {
    		do_action("search_filter_update_post_cache", $post_id);
    	}    
    }
    
    add_action('acf/save_post', 'my_acf_save_post', 20);

    The is_admin line makes sure that the cache is only updated on the frontend and in ajax requests (as we know creating posts in the admin area works fine)

    The only issue with this approach is on the frontend, S&F will build the cache for a new post twice, first without any meta data, and then on acf/save_post, with the correct data.

    It should add very little overhead, but its worth pointing out (I may look into making it easy to remove S&F cache from the the WP save_post action, unless of course ACF get the WP save_post action working on the front end).

    Let me know how you get on.

    Sanna, I added this to the first site I mentioned that I have been working on and you can see the posts are updating correctly 🙂

    Best

    #94147

    Ross
    Keymaster

    Hi Sanna

    Ok I have some good and bad news for you.

    I’ve tracked down the issue, but I don’t know whats causing it (and its not S&F).

    I’ll try to explain a little:

    To detect when a new post is created we use the WP action save_post

    add_action( 'save_post', 'post_saved', 10000 );

    I’ve added this to the bottom of your functions.php.

    Then inside this function, we can use get_post_meta to retrieve the value of your date (S&F needs to do this to be able to store this info).

    The problem is, using get_post_meta($post_id, 'datum', true); is failing (returning nothing, like it doesn’t yet exist), but ONLY when you create / update a post from the frontend, and only when you have a load of posts in your database.

    When you add a new report via admin, the save_post is triggered, but the date variable is there.

    When you do it on a site with a low number of posts, its also there.

    Its just in this circumstance using save_post and get_post_meta does not work.

    Where to go from here?

    I think its a good idea to get in touch with your host, because of the number of posts looking like its an issue, then it could be server related.

    If that doesn’t work, I would get in touch with ACF, and ask why get_post_meta doesn’t return anything in the save_post action.

    Once you can get this working in your setup, then S&F can also detect this data, and use it properly.

    A quick way to test this yourself:

    1) I’ve added code to the bottom of your functions.php which logs every save_post on your site, with the date and post ID.
    2) All save info is being logged to wp-content/debug.log
    3) To test this, open wp-content/debug.log, empty the file and save it (and upload the blank file back to the server) – this is just to clear the log and make it more readable.
    4) Now goto your frontend form, and create a new report.
    5) Download a new copy of debug.log and check it, you will find a line like:
    [02-Mar-2017 20:32:02 UTC] Post saved, ID: 3761, meta date:
    Notice, there is no meta date.
    6) Goto admin, and create a new report, check debug.log again (make sure to grab a fresh copy), you will see a line like:
    [02-Mar-2017 20:36:06 UTC] Post saved, ID: 3764, meta date: 20170312
    Notice how this time the post has the date with it.

    For some reason when you had only a few posts, beheviour was the same a step (6), but now with many posts its doing step (5).

    I hope that helps!

    #89046

    Anonymous
    Inactive

    Just to make sure I got it right. The local file is now:

    <?php
    namespace Elementor\TemplateLibrary;
    
    use Elementor\DB;
    use Elementor\Plugin;
    use Elementor\Settings;
    use Elementor\User;
    
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    class Source_Local extends Source_Base {
    
    	const CPT = 'elementor_library';
    
    	const TAXONOMY_TYPE_SLUG = 'elementor_library_type';
    
    	const TYPE_META_KEY = '_elementor_template_type';
    
    	private static $_template_types = [ 'page', 'section' ];
    
    	public static function get_template_type( $template_id ) {
    		return get_post_meta( $template_id, self::TYPE_META_KEY, true );
    	}
    
    	public static function is_base_templates_screen() {
    		global $current_screen;
    
    		if ( ! $current_screen ) {
    			return false;
    		}
    
    		return 'edit' === $current_screen->base && self::CPT === $current_screen->post_type;
    	}
    
    	public static function add_template_type( $type ) {
    		self::$_template_types[] = $type;
    	}
    
    	public function get_id() {
    		return 'local';
    	}
    
    	public function get_title() {
    		return __( 'Local', 'elementor' );
    	}
    
    	public function register_data() {
    		$labels = [
    			'name' => _x( 'My Library', 'Template Library', 'elementor' ),
    			'singular_name' => _x( 'Template', 'Template Library', 'elementor' ),
    			'add_new' => _x( 'Add New', 'Template Library', 'elementor' ),
    			'add_new_item' => _x( 'Add New Template', 'Template Library', 'elementor' ),
    			'edit_item' => _x( 'Edit Template', 'Template Library', 'elementor' ),
    			'new_item' => _x( 'New Template', 'Template Library', 'elementor' ),
    			'all_items' => _x( 'All Templates', 'Template Library', 'elementor' ),
    			'view_item' => _x( 'View Template', 'Template Library', 'elementor' ),
    			'search_items' => _x( 'Search Template', 'Template Library', 'elementor' ),
    			'not_found' => _x( 'No Templates found', 'Template Library', 'elementor' ),
    			'not_found_in_trash' => _x( 'No Templates found in Trash', 'Template Library', 'elementor' ),
    			'parent_item_colon' => '',
    			'menu_name' => _x( 'My Library', 'Template Library', 'elementor' ),
    		];
    
    		$args = [
    			'labels' => $labels,
    			'public' => true,
    			'rewrite' => false,
    			'show_ui' => true,
    			'show_in_menu' => false,
    			'show_in_nav_menus' => false,
    			'exclude_from_search' => true,
    			'capability_type' => 'post',
    			'hierarchical' => false,
    			'supports' => [ 'title', 'thumbnail', 'author', 'elementor' ],
    		];
    
    		register_post_type(
    			self::CPT,
    			apply_filters( 'elementor/template_library/sources/local/register_post_type_args', $args )
    		);
    
    		$args = [
    			'hierarchical' => false,
    			'show_ui' => false,
    			'show_in_nav_menus' => false,
    			'show_admin_column' => true,
    			'query_var' => is_admin(),
    			'rewrite' => false,
    			'public' => false,
    			'label' => _x( 'Type', 'Template Library', 'elementor' ),
    		];
    
    		register_taxonomy(
    			self::TAXONOMY_TYPE_SLUG,
    			self::CPT,
    			apply_filters( 'elementor/template_library/sources/local/register_taxonomy_args', $args )
    		);
    	}
    
    	public function register_admin_menu() {
    		add_submenu_page(
    			Settings::PAGE_ID,
    			__( 'My Library', 'elementor' ),
    			__( 'My Library', 'elementor' ),
    			'edit_pages',
    			'edit.php?post_type=' . self::CPT
    		);
    	}
    
    	public function get_items( $args = [] ) {
    		$templates_query = new \WP_Query(
    			[
    				'post_type' => self::CPT,
    				'post_status' => 'publish',
    				'posts_per_page' => -1,
    				'orderby' => 'title',
    				'order' => 'ASC',
    				'meta_query' => [
    					[
    						'key' => self::TYPE_META_KEY,
    						'value' => self::$_template_types,
    					],
    				],
    			]
    		);	
    
    		$templates = [];
    do_action("search_filter_query_posts", 4330);	
    		if ( $templates_query->have_posts() ) {
    			foreach ( $templates_query->get_posts() as $post ) {
    				$templates[] = $this->get_item( $post->ID );
    			}
    		}
    
    		if ( ! empty( $args ) ) {
    			$templates = wp_list_filter( $templates, $args );
    		}
    
    		return $templates;
    	}
    
    	public function save_item( $template_data ) {
    	    if ( ! in_array( $template_data['type'], self::$_template_types ) ) {
    			return new \WP_Error( 'save_error', 'Invalid template type <code>' . $template_data['type'] . '</code>' );
    		}
    
    		$template_id = wp_insert_post( [
    			'post_title' => ! empty( $template_data['title'] ) ? $template_data['title'] : __( '(no title)', 'elementor' ),
    			'post_status' => 'publish',
    			'post_type' => self::CPT,
    		] );
    
    		if ( is_wp_error( $template_id ) ) {
    			return $template_id;
    		}
    
    		Plugin::instance()->db->set_edit_mode( $template_id );
    
    		Plugin::instance()->db->save_editor( $template_id, $template_data['data'] );
    
    		$this->save_item_type( $template_id, $template_data['type'] );
    
    		do_action( 'elementor/template-library/after_save_template', $template_id, $template_data );
    
    		do_action( 'elementor/template-library/after_update_template', $template_id, $template_data );
    
    		return $template_id;
    	}
    
    	public function update_item( $new_data ) {
    		Plugin::instance()->db->save_editor( $new_data['id'], $new_data['data'] );
    
    		do_action( 'elementor/template-library/after_update_template', $new_data['id'], $new_data );
    
    		return true;
    	}
    
    	/**
    	 * @param int $item_id
    	 *
    	 * @return array
    	 */
    	public function get_item( $item_id ) {
    		$post = get_post( $item_id );
    
    		$user = get_user_by( 'id', $post->post_author );
    
    		$data = [
    			'template_id' => $post->ID,
    			'source' => $this->get_id(),
    			'type' => self::get_template_type( $post->ID ),
    			'title' => $post->post_title,
    			'thumbnail' => get_the_post_thumbnail_url( $post ),
    			'date' => mysql2date( get_option( 'date_format' ), $post->post_date ),
    			'author' => $user->display_name,
    			'categories' => [],
    			'keywords' => [],
    			'export_link' => $this->_get_export_link( $item_id ),
    			'url' => get_permalink( $post->ID ),
    		];
    
    		return apply_filters( 'elementor/template-library/get_template', $data );
    	}
    
    	public function get_content( $item_id, $context = 'display' ) {
    		$db = Plugin::instance()->db;
    
    		// TODO: Validate the data (in JS too!)
    		if ( 'display' === $context ) {
    			$data = $db->get_builder( $item_id );
    		} else {
    			$data = $db->get_plain_editor( $item_id );
    		}
    
    		$data = $this->replace_elements_ids( $data );
    
    		return $data;
    	}
    
    	public function delete_template( $item_id ) {
    		wp_delete_post( $item_id, true );
    	}
    
    	public function export_template( $item_id ) {
    		$template_data = $this->get_content( $item_id, 'raw' );
    
    		$template_data = $this->process_export_import_data( $template_data, 'on_export' );
    
    		if ( empty( $template_data ) )
    			return new \WP_Error( '404', 'The template does not exist' );
    
    		// TODO: More fields to export?
    		$export_data = [
    			'version' => DB::DB_VERSION,
    			'title' => get_the_title( $item_id ),
    			'type' => self::get_template_type( $item_id ),
    			'data' => $template_data,
    		];
    
    		$filename = 'elementor-' . $item_id . '-' . date( 'Y-m-d' ) . '.json';
    		$template_contents = wp_json_encode( $export_data );
    		$filesize = strlen( $template_contents );
    
    		// Headers to prompt "Save As"
    		header( 'Content-Type: application/octet-stream' );
    		header( 'Content-Disposition: attachment; filename=' . $filename );
    		header( 'Expires: 0' );
    		header( 'Cache-Control: must-revalidate' );
    		header( 'Pragma: public' );
    		header( 'Content-Length: ' . $filesize );
    
    		// Clear buffering just in case
    		@ob_end_clean();
    
    		flush();
    
    		// Output file contents
    		echo $template_contents;
    
    		die;
    	}
    
    	public function import_template() {
    		$import_file = $_FILES['file']['tmp_name'];
    
    		if ( empty( $import_file ) )
    			return new \WP_Error( 'file_error', 'Please upload a file to import' );
    
    		$content = json_decode( file_get_contents( $import_file ), true );
    		$is_invalid_file = empty( $content ) || empty( $content['data'] ) || ! is_array( $content['data'] );
    
    		if ( $is_invalid_file )
    			return new \WP_Error( 'file_error', 'Invalid File' );
    
    		$content_data = $this->process_export_import_data( $content['data'], 'on_import' );
    
    		$item_id = $this->save_item( [
    			'data' => $content_data,
    			'title' => $content['title'],
    			'type' => $content['type'],
    		] );
    
    		if ( is_wp_error( $item_id ) )
    			return $item_id;
    
    		return $this->get_item( $item_id );
    	}
    
    	public function post_row_actions( $actions, \WP_Post $post ) {
    		if ( self::is_base_templates_screen() ) {
    			if ( $this->is_template_supports_export( $post->ID ) ) {
    				$actions['export-template'] = sprintf( '<a href="%s">%s</a>', $this->_get_export_link( $post->ID ), __( 'Export Template', 'elementor' ) );
    			}
    
    			unset( $actions['inline hide-if-no-js'] );
    		}
    
    		return $actions;
    	}
    
    	public function admin_import_template_form() {
    		if ( ! self::is_base_templates_screen() ) {
    			return;
    		}
    		?>
    		<div id="elementor-hidden-area">
    			<a id="elementor-import-template-trigger" class="page-title-action"><?php _e( 'Import Template', 'elementor' ); ?></a>
    			<div id="elementor-import-template-area">
    				<div id="elementor-import-template-title"><?php _e( 'Choose an Elementor template JSON file, and add it to the list of templates available in your library.', 'elementor' ); ?></div>
    				<form id="elementor-import-template-form" method="post" action="<?php echo admin_url( 'admin-ajax.php' ); ?>" enctype="multipart/form-data">
    					<input type="hidden" name="action" value="elementor_import_template">
    					<fieldset id="elementor-import-template-form-inputs">
    						<input type="file" name="file" accept="application/json" required>
    						<input type="submit" class="button" value="<?php _e( 'Import Now', 'elementor' ); ?>">
    					</fieldset>
    				</form>
    			</div>
    		</div>
    		<?php
    	}
    
    	public function block_template_frontend() {
    		if ( is_singular( self::CPT ) && ! User::is_current_user_can_edit() ) {
    			wp_redirect( site_url(), 301 );
    			die;
    		}
    	}
    
    	public function is_template_supports_export( $template_id ) {
    		return apply_filters( 'elementor/template_library/is_template_supports_export', true, $template_id );
    	}
    
    	private function _get_export_link( $item_id ) {
    		return add_query_arg(
    			[
    				'action' => 'elementor_export_template',
    				'source' => $this->get_id(),
    				'template_id' => $item_id,
    			],
    			admin_url( 'admin-ajax.php' )
    		);
    	}
    
    	public function on_save_post( $post_id, $post ) {
    		if ( self::CPT !== $post->post_type ) {
    			return;
    		}
    
    		if ( self::get_template_type( $post_id ) ) { // It's already with a type
    			return;
    		}
    
    		$this->save_item_type( $post_id, 'page' );
    	}
    
    	private function save_item_type( $post_id, $type ) {
    		update_post_meta( $post_id, self::TYPE_META_KEY, $type );
    
    		wp_set_object_terms( $post_id, $type, self::TAXONOMY_TYPE_SLUG );
    	}
    
    	/**
    	 * @param $query \WP_Query
    	 */
    	public function admin_query_filter_types( $query ) {
    		if ( ! function_exists( 'get_current_screen' ) ) {
    			return;
    		}
    
    		$library_screen_id = 'edit-' . self::CPT;
    		$current_screen = get_current_screen();
    
    		if ( ! isset( $current_screen->id ) || $library_screen_id !== $current_screen->id ) {
    			return;
    		}
    
    		$query->query_vars['meta_key'] = self::TYPE_META_KEY;
    		$query->query_vars['meta_value'] = self::$_template_types;
    	}
    
    	private function _add_actions() {
    		if ( is_admin() ) {
    			add_action( 'admin_menu', [ $this, 'register_admin_menu' ], 50 );
    			add_filter( 'post_row_actions', [ $this, 'post_row_actions' ], 10, 2 );
    			add_action( 'admin_footer', [ $this, 'admin_import_template_form' ] );
    			add_action( 'save_post', [ $this, 'on_save_post' ], 10, 2 );
    			add_action( 'parse_query', [ $this, 'admin_query_filter_types' ] );
    		}
    
    		add_action( 'template_redirect', [ $this, 'block_template_frontend' ] );
    	}
    
    	public function __construct() {
    		parent::__construct();
    
    		$this->_add_actions();
    	}
    }
    
    #59734

    Anonymous
    Inactive

    Hi,

    Sorry for late answer, I was off 2 days.

    So following your advice, I’ve added an function to save_post hook but it does not seem to solve my issue. After the import I can’t have any result on my search.

    Do I miss something ?

    function update_sf_cache( $post_id ) {

    $post_type = get_post_type($post_id);

    // If this isn’t a ‘used_vehicle’ post, exit.
    if ( “used_vehicle” != $post_type ) return;

    // Update the search & filter cache for this post.
    do_action(‘search_filter_update_post_cache’, $post_id);
    }
    add_action(‘save_post’, ‘update_sf_cache’);

Viewing 6 results - 11 through 16 (of 16 total)