Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Tinify service available outside wp-admin #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Make Tinify service available outside wp-admin #24

wants to merge 2 commits into from

Conversation

realrellek
Copy link

Making Tinify available outside wp-admin. For example if you sideload an image using an API or a frontend plugin (e.g. contact forms). No performance impact because compressor is only initialised if we actually have to process media, in which case we have to do quite heavy work anyway.

Making Tinify available outside wp-admin. For example if you sideload an image using an API or a frontend plugin (e.g. contact forms). No performance impact because compressor is only initialised if we actually have to process media, in which case we have to do quite heavy work anyway.
@rkoopmans
Copy link
Contributor

Hi Realrellek,

Thanks for showing interest in this project and investing the time to extend the code base.

How would you be integrating the plugin with the code above? Knowing how you would like to use it would give us a better view and would also help in documenting such feature.

I think it might be better to create a new init funciton for your use case, as defined here:

$this->init_compressor();

With this change we are modifying the behaviour of the get_compressor function which is used in multiple places.

Please share some example usage code we can discuss about the implementation

@realrellek
Copy link
Author

Hi rkoopmans,

sure.

First of all, I don't think it changes the behaviour of the get_compressor. Because we have another issue: The compressor is only initialised when admin_init() is called (or xmlrpc_init or ajax_init). Which only happens when we have an xmlrpc-request or an ajax request or we are in wp-admin. see:

add_action( 'admin_init', $this->get_method( 'ajax_init' ) );

Otherwise, we do not have the compressor available.

What we do have available, are the hooks. Most notably for wp_generate_attachment_metadata in class-tiny-plugin.php:

add_filter( 'wp_generate_attachment_metadata',

What that means is, if you use any WP function to sideload an image (or upload it using WP API) outside WP Admin, the compression method is called but will fail because of a missing compressor.

My suggestion would initialise a compressor when it is needed. It does take into account that initialising a compressor without the need to can be a performance issue (especially when it fails for whatever reason), so I'd only do it when necessary.


I do not like the idea of having an own init() function because that would bind my code to yours. It is much more elegant if I can just use WP API to sideload an image and magic just happens.


Example code. Aside from countless support threads on wp.org asking for support for say contact forms or others that allow uploading from the frontend.

But my specific problem is as follows:

I have 2 WP installations. One is the master and on is the slave. On the master, the editor writes an article (with images and tables and everything). And editor clicks a link. Link beams everything to the slave install where another editor can modify the content but keeps images.

I only copy the original size over and have WP do the crops. But those are not optimized then. Which I want Tinify to do.

Relevant code:

`$featuredID = null;

	// So the featured image is in featured['uri'], let's see if we have that
	// already. If so, we can happily skip this.
	$featured = $data['media']['featured'];
	
	$results = $wpdb->get_results( "SELECT post_id from $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = '".esc_sql($featured['uri'])."'", ARRAY_A );
	if(!empty($results[0]['post_id'])) {
		
		$featuredID = $results[0]['post_id'];
		
	}
	
	//var_dump($featuredID);die();
	
	if(is_null($featuredID)) {
		
		$featured['url'] = str_replace('http://spar.localdev/', 'https://www.sparneuwagen.de/', $featured['url']);
		$featured['url'] = str_replace('-scaled.', '.', $featured['url']);
		
		// Gotta upload this crap.
		
		$tmp = download_url($featured['url']);
		$fileArray = array(
			'name' => basename($featured['url']),
			'tmp_name' => $tmp
		);
		
		if(is_wp_error($tmp)) {
			
			// Well facc.
			echo 'error';
			var_dump($tmp);
			@unlink($fileArray['tmp_name']);
			return $tmp;
			
		}
		
		$postData = array(
			
			'post_author' => $featured['author'],
			'post_date' => $featured['post_date'],
			'post_date_gmt' => $featured['post_date_gmt'],
			'post_content' => $featured['post_content'],
			'post_title' => $featured['post_title'],
			'post_excerpt' => $featured['post_excerpt'],
			'post_name' => $featured['post_name'],
			'post_modified' => $featured['post_modified'],
			'post_modified_gmt' => $featured['post_modified_gmt']
			
		);
		
		$featuredID = media_handle_sideload($fileArray, 0, 'null', $postData);
		add_metadata('post', $featuredID, '_wp_attachment_image_alt', $featured['alt'], true);
		
		
	}`

The actual upload happens in the last few lines with media_handle_sideload and add_metadata.

As we are not in the wp-admin, because we do not need to, because that's just 2 servers talking to each other, no compressor is available and thus no tinification is happening. Which leads me to babysit all migrations because each time I have to check and manually optimize the images. Which I would like to avoid.

I hope that helps you a little bit :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants