Jump to content

Page publish hook


Nico Knoll
 Share

Recommended Posts

Hi,

I'm writing a module which needs to hook to the page publish process. (It should post a link on facebook and a link on twitter if a page is published). So is there a hook for this already? And if yes, then which?

Greets,

Nico

Link to comment
Share on other sites

It would probably be something like this

<?php 		
// add a hook after the $pages->save, to issue a notice every time a page is saved
$this->pages->addHookAfter('save', $this, 'example1'); 

But note that this will hook on every page save, even on page updates or when comments are added.

/Jasper

Link to comment
Share on other sites

There isn't currently a hook, but I was going to write one (for Twitter at least) and use Tijs Verkoyen's Twitter oAuth class that I've used for other stuff before: http://classes.verkoyen.eu/twitter_oauth

You will need a hidden custom field in which to store the twitter status ID (and Facebook's equivalent, but I can't help you there as I inly know about Twitter in terms of the above class).

For such a module, the steps I would take in the code are:

  • Add hook after page save
  • Check to see whether page status is published
  • Check the hidden twitter status field - this stores the unique ID of the tweet
  • If it's empty, add a tweet
  • If it's not empty, do nothing

Similarly, you might want to automatically delete a tweet if a page is deleted, otherwise you will have issues with tweets linking back to non-existent pages. Personally I would make this a configuration setting in the module so you can decide what to do for your particular scenario.

The steps when a page is deleted (and I would say deleted from the trash - not just moved to the trash - just in case it's an error) are:

  • Add hook after page save
  • Check to see whether page has been deleted
  • Check the hidden twitter status field - this stores the unique ID of the tweet
  • If it's empty, do nothing
  • If it's not empty, delete the tweet

If you don't mind waiting a few hours, I need to build this module anyway so I might just do it this afternoon. Then you could use it as a basis for a separate Facebook version if you like?

Link to comment
Share on other sites

Well, I solved this like this:

public function init() {
	parent::init();
	$this->addHookBefore('ProcessPageEdit::execute', $this, 'saveAfter'); 
}

public function saveAfter(HookEvent $event) {

	if($this->input->post->id > 0 && count($_POST)) {
		$db_page = (($this->pages->get('id='.$_GET['id']) instanceof NullPage) ? false : $this->pages->get('id='.$_GET['id']));

		if($this->input->post->submit_publish && (bool)$db_page->is(Page::statusUnpublished)) {
			if($this->facebookUser && $this->facebookToken) {
				$this->shareFacebook('http://www.example.com/');
			}

		}			

	}
}

The Facebook part is working right now. So if you would create the twitter part it would be great.

/ Nico

Link to comment
Share on other sites

I checked and there isn't currently a way to detect the status change from unpublished > published. Unless your module keeps track of it on its own by monitoring the status at load and save. So I'm going to put adding a Pages::published() on my to-do list, as it looks like it's something we need.

Shorter term, what you are currently doing Nico is one way to accomplish it. But note that's only going to detect it interactively, and not for other API usage (which may be fine). I would just suggest changing this line:

$db_page = (($this->pages->get('id='.$_GET['id']) instanceof NullPage) ? false : $this->pages->get('id='.$_GET['id']));

to this:

<?php
$db_page = wire('process')->getPage();
if(!$db_page || !$db_page->id || !$db->page->editable()) return;  
Link to comment
Share on other sites

Hi Nico

Sorry for the slight delay with this. I said a couple of hours, but it's now been several days (forum upgrade being my main distraction).

If you can hold on a bit longer, I will be looking at this again on Saturday definitely, but this week is a bit hectic at work for me to looko into it sooner unfortunately.

Link to comment
Share on other sites

(It should post a link on facebook and a link on twitter if a page is published).

This seems like a very interesting module. Does it currently post for the user that is logged in, or is it possible to post for some "Facebook page" wall? Meaning that if I manage cocacola.com site is it possible to post to cocacola facebook wall instead of my personal fb-account?

Link to comment
Share on other sites

This seems like a very interesting module. Does it currently post for the user that is logged in, or is it possible to post for some "Facebook page" wall? Meaning that if I manage cocacola.com site is it possible to post to cocacola facebook wall instead of my personal fb-account?

Both. You can add account-IDs and and page-IDs.

Link to comment
Share on other sites

Just finished some more stuff for the ProcessWire site which I'm about to send to ryan for his perusal before it gets uploaded, possibly early next week. Just a bit of forum/site integration that looks quite nice, but the code is a bit sloppy so I'll not upload it until it's been checked over ;)

I'll be working on the Twitter module again in about 20 minutes and hopefully it won't take me long to complete - maybe an hour or two. I should warn you that my self-imposed deadlines usually suck though, sorry :D

Link to comment
Share on other sites

Just a quick update - this is coming along nicely. What I'm just trying to add now is a nice select so we can select the sections who'se cihld pages should trigger a post to Twitter (otherwise they all do).

If I can't figure it out with ASMSelect in the next 30 minutes then I'll just do it as a comma-separated list for now.

Oh, and I included bit.ly to optionally shorten URLs as well.

Link to comment
Share on other sites

I saw it :)

My Code isn't that huge right now (waited for you ;)). it's like this:

<?php

/**
* Social Share
*
* By Nico Knoll (http://www.nico-knoll.de/)
*
*/

class SocialShare extends Process implements Module {

public static function getModuleInfo() {
return array(
'title' => 'Social Share',
'version' => '1.01',
'summary' => 'Let you see a preview of a page.',
'autoload' => true
);
}

private $facebookUser = '';
private $facebookToken = '';
private $twitterUser = '';
private $twitterToken = '';

public function init() {
parent::init();
//$this->addHook('ProcessPageEdit::buildForm', $this, 'addButton');
$this->addHookBefore('ProcessPageEdit::execute', $this, 'saveAfter');

}

public function saveAfter(HookEvent $event) {
//var_dump($event->object);
//var_dump($this->pages);

//var_dump($this->config->input->post->id, $_POST );

if($this->input->post->id > 0 && count($_POST)) {

$db_page = wire('process')->getPage();
if(!$db_page || !$db_page->id || !$db_page->editable()) return;  

if($this->input->post->submit_publish && (bool)$db_page->is(Page::statusUnpublished)) {
if($this->facebookUser && $this->facebookToken) {
$this->shareFacebook('http://www.stadtpirat.net/');
}

}

}
}

public function shareFacebook($share_url, $msg = '') {
if(!$this->facebookUser || !$this->facebookToken) return false;

$url = "https://graph.facebook.com/".$this->facebookUser."/links";
$attachment =  array(
'access_token' => $this->facebookToken,
'message' => $msg,
'link' => $share_url
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
$result = curl_exec($ch);
curl_close ($ch);
return $result;

}

}
Link to comment
Share on other sites

  • 1 year later...
  • 6 months later...

I checked and there isn't currently a way to detect the status change from unpublished > published. Unless your module keeps track of it on its own by monitoring the status at load and save. So I'm going to put adding a Pages::published() on my to-do list, as it looks like it's something we need.....

Hi Ryan,

Will you be getting round to this any time soon? I currently would like this functionality for this issue in 'Blog'...otherwise I would have to possibly create an autoload module as shown here. Thanks.

  • Like 1
Link to comment
Share on other sites

@kongondo the functionality was added a little while back and is already in the stable 2.4.0. See these four hooks in Pages.php

Great. Thanks! Thinking about it more clearly now, I will still need an autoload module to use these with? This is for post pages that will be edited in the normal way using the Page Tree. I appreciate any pointers, thanks! Hooks are still a mystery to me :D

Link to comment
Share on other sites

I would stick them in an autoload module. If you have a few small bits of code and are worried about creating lots of small autoload modules like I often do that are only useful on certain sites then you could create a little utility module just for that site and chuck them all in there.

It's more efficient as well than having lots of separate tiny autoload modules.

  • Like 1
Link to comment
Share on other sites

  • 4 months later...

Hi.

I have been looking for a way to "post to facebook", after some digging in the FB SDK I got it it somewhat working, problem is that I need to logon to facebook every time as I need to have a valid session in order to perform the post.

Anyway you guys seem to have solved it, but I cant find the module in the module library.

Basically what I want to acheave is that I have a checkbox field when creating a page, when that is checked the and the page is saved it you be posted to a specific facebook page, not my personal one.

@Nico, do you have the code and perhaps some simple instructions somewhere ?

Thanks / Ronnie

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...