Custom WordPress New User E-Mail from Template - html

From a custom function to register users, I inserted this function to generate and overwrite the WordPress system email for new user notification.
function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
global $wpcargo;
$user_url = stripslashes( $user->user_url );
$user_login = stripslashes( $user->user_login );
$user_email = stripslashes( $user->user_email );
$user_firstname = stripslashes( $user->user_firstname );
$user_last_name = stripslashes( $user->user_last_name );
$user_pass = stripslashes( $user->user_pass );
$message = file_get_contents('../email/mail-template.php');
$wp_new_user_notification_email['subject'] = sprintf( '[%s] Welcome.', $blogname );
$wp_new_user_notification_email['headers'] = array('Content-Type: text/html; charset=UTF-8');
$wp_new_user_notification_email['message'] = $message;
return $wp_new_user_notification_email;
}
add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 10, 3 );
Emails are being sent but I cannot get user data printed in the PHP / HTML file.
<p style="margin-top:0;margin-bottom:12px;"><b>Name</b>: <?php $user_firstname; ?></p>
I'm wrong with the code, any suggestions?

I would use locate_template for the email contents and output buffering rather than file_get_contents - also as #Zak pointed out, you have to echo your output otherwise it won't show up.
function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
global $wpcargo;
$user_url = stripslashes( $user->user_url );
$user_login = stripslashes( $user->user_login );
$user_email = stripslashes( $user->user_email );
$user_firstname = stripslashes( $user->user_firstname );
$user_last_name = stripslashes( $user->user_last_name );
$user_pass = stripslashes( $user->user_pass );
ob_start();
include( locate_template( '/email/mail-template.php' ); // This path may vary depending on your setup.
$wp_new_user_notification_email['message'] = ob_get_clean();
$wp_new_user_notification_email['subject'] = sprintf( '[%s] Welcome.', $blogname );
$wp_new_user_notification_email['headers'] = array('Content-Type: text/html; charset=UTF-8');
return $wp_new_user_notification_email;
}
add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 10, 3 );
Within your template you want to make sure you echo and esc_html() for anything that is being outputted.
<?php echo esc_html( $user_firstname ); ?>

Related

Reset meta value to zero in WP

I am using this code to enable "product_visit_count".
I wish to reset the said meta value to zero, but i am unsure and do not want to mess my database.
any pointers?
add_action( 'woocommerce_before_single_product', 'prefix_save_product_views' );
function prefix_save_product_views( ) {
$product_id = get_the_ID();
$increment = 1;
$current_visit_count = get_post_meta( $product_id, 'product_visit_count', true );
$total_visit_count = (int)$current_visit_count + $increment;
update_post_meta( $product_id, 'product_visit_count', $total_visit_count );
}

How to make a json from youtube api v3 with a wp_cron

I'm doing a cron job in wordpress and it only worked once, it sent me an email but it never created the json file.
I want to save in a json file data from youtube api v3 in order not make petitions each time because youtube limit the petitions so I thought it will be convenient make a cache job with wp cron but this code is not working. I not sure where is the problem.
function isa_add_every_three_minutes( $schedules ) {
$schedules['every_three_minutes'] = array(
'interval' => 180,
'display' => __( 'Every 3 Minutes', 'textdomain' )
);
return $schedules;
}
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );
if ( !wp_next_scheduled('hook_saveVideos') ) {
wp_schedule_event( time(), 'every_three_minutes', 'hook_saveVideos' );
}
function saveVideos() {
$channel_id = "channelExample";
$user_id = "example";
$url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=30&playlistId=".$cv_id."&key=".$user_id;
$data = file_get_contents($url);
$videos = json_decode($data, true);
$videos = $videos['items'];
$dVideos = array();
for ($i=0; $i < count($videos) ; $i++) {
$title = $videos[$i]['snippet']['title'];
$description = $videos[$i]['snippet']['description'];
$videoId = $videos[$i]['snippet']['resourceId']['videoId'];
$id = "v".$i;
$dVideos[$id] = array("title"=>$title, "description"=>$description, "videoId"=>$videoId);
}
$ruta = "http://localhost/example/cache";
$fp = fopen($ruta, 'w');
fwrite($fp, json_encode($dVideos ));
fclose($fp);
wp_mail('user#example.com', 'Mail mandado por el cron', 'Hola, esto es un correo enviado automáticamente');
}

Google API for analytics invalid grant but works sometimes

I've been on this problem now for 2 days and have tried looking under google code and stackoverflow but still can come up with an answer.
My problem is when I try my google analytics api I get "Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'"
But the weird part is that some times it will work. rarely but if I refresh and keep trying it, it will output.
The only thing I could find is that it could be the refresh token limit has been exceeded
Attached is my code if someone could help me out or point me to the right direction.
Thanks!
<?php
session_start();
require_once 'Google_Client.php';
require_once 'Google_AnalyticsService.php';
require_once 'config.php';
$keyFile = 'key.p12';
$client = new Google_Client();
$client->setApplicationName("test");
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
$client->setAssertionCredentials(new Google_AssertionCredentials(
GOOGLE_SERVICE_ACCOUNT,
array('https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents($keyFile))
);
$client->setClientId(GOOGLE_CLIENT_ID);
$client->setAccessType('offline');
$client->setUseObjects(true);
$service = new Google_AnalyticsService($client);
try {
$results = $service->data_ga->get(
'ga:44444444',
date('Y-m-d', strtotime('-30 days '.date('Y-m-d', strtotime('-1 day '.date('Y-m- d'))))),
date('Y-m-d', strtotime('-1 day '.date('Y-m-d'))),
'ga:visits,ga:newVisits',
/*array(
'dimensions' => 'ga:source,ga:keyword',
'sort' => '-ga:visits,ga:keyword',
'filters' => 'ga:medium==organic',
'max-results' => '25'
)*/
array('dimensions' => 'ga:date')
);
} catch (Google_ServiceException $e) {
// echo $e->getMessage();
}
if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
}
$dateParsePattern = '/"Date.parse\(\\\"((\d{4})-(\d{2})-(\d{2})) UTC\\\"\)"/';
$dateParseReplacement = 'Date.parse("$1 UTC")';
$allVisitsItems = array();
$newVisitorsItems = array();
if ($results && count($results->getRows()) > 0) {
foreach ($results->getRows() as $row) {
$date = 'Date.parse("'.date("Y-m-d", strtotime($row[0])).' UTC")';
$allVisitsItems[] = array($date, intval(htmlspecialchars($row[1], ENT_NOQUOTES)));
$newVisitorsItems[] = array($date, intval(htmlspecialchars($row[2], ENT_NOQUOTES)));
}
}
header('Content-Type: application/json');
?>
<?php echo preg_replace($dateParsePattern, $dateParseReplacement, json_encode($allVisitsItems)) ?>
Edit - It's not the NTP, when I echoed date('l jS \of F Y h:i:s A'); it matched up.
The following works to generate output - make sure you are running PHP 5.3 or higher.
<?php
require_once './src/Google_Client.php';
require_once './src/contrib/Google_AnalyticsService.php';
$path_to_keyfile = '.p12';
$service_account_email = '7#developer.gserviceaccount.com';
$client_id = '7.apps.googleusercontent.com';
$analytics_profile_id = 'ga:IN URL OF ANALYTICS';
$client = new Google_Client();
$client->setApplicationName("API TEST");
$client->setAssertionCredentials(
new Google_AssertionCredentials(
$service_account_email,
array('https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents($path_to_keyfile)
)
);
$client->setClientId($client_id);
$client->setAccessType('offline_access');
$service = new Google_AnalyticsService($client);
$from = date('Y-m-d', strtotime('-30 days '.date('Y-m-d', strtotime('-1 day '.date('Y-m-d'))))); // 30 days
$to = date('Y-m-d', strtotime('-1 day '.date('Y-m-d'))); // yesterday
$dateParsePattern = '/"Date.parse\(\\\"((\d{4})-(\d{2})-(\d{2})) UTC\\\"\)"/';
$dateParseReplacement = 'Date.parse("$1 UTC")';
$allVisitsItems = array();
$newVisitorsItems = array();
try {
$data = $service->data_ga->get(
//
$analytics_profile_id,
$from,
$to,
'ga:visits,ga:newVisits',
array('dimensions' => 'ga:date')
);
if($data && $data['totalResults'] > 0) {
foreach($data['rows'] as $row) {
$date = 'Date.parse("'.date("Y-m-d", strtotime($row[0])).' UTC")';
$allVisitsItems[] = array($date, intval(htmlspecialchars($row[1], ENT_NOQUOTES)));
$newVisitorsItems[] = array($date, intval(htmlspecialchars($row[2], ENT_NOQUOTES)));
}
}
header('Content-Type: application/json');
?>
<?php echo preg_replace($dateParsePattern, $dateParseReplacement, json_encode($allVisitsItems)) ?>
<?php echo preg_replace($dateParsePattern, $dateParseReplacement, json_encode($newVisitorsItems)) ?>
<?php
} catch(Exception $e) {
echo 'There was an error : - ' . $e->getMessage();
}

Enable a mediawiki function to render wikitext

My mediawiki version is 1.16.5. I have a function that selects a random quote from the database and displays it on a wiki page. Italics and bold are rendered when I use html, however, I want to be able to use wikitext, specifically external links format, i.e.
[http://mediawiki.org MediaWiki]
Currently, if using something like the above, it is not rendered and displays literally. There is some reference on what is needed here: http://www.mediawiki.org/wiki/Manual:Tag_extensions#How_do_I_render_wikitext_in_my_extension.3F but I do not know how to implement this.
Here is the code:
function wfGetQuote() {
$randstr = wfRandom();
$row = selectRandomQuoteFromDB( $randstr );
if( !$row )
$row = selectRandomQuoteFromDB( "0" );
if( $row ) {
list( $quote, $attribution ) = explode( "\n", $row->quote_text );
return '<div id="trrandomquote"><div id="trquote">'. $quote .'</div> <span>'. $attribution .'</span></div>';
}
else
return 'Error: No quote found';
}
function selectRandomQuoteFromDB( $randstr ) {
global $wgUser, $site;
$lang = $wgUser->getOption( 'language' );
if( $site == 'wiki' )
$lang = 'en';
$dbr = wfGetDB( DB_SLAVE );
$use_index = $dbr->useIndexClause( 'quote_random' );
$quote = $dbr->tableName( 'quote' );
$sql = "SELECT quote_text
FROM $quote $use_index
WHERE quote_random >= $randstr
AND quote_lang = '$lang'";
$sql = $dbr->limitResult( $sql, 1, 0 );
$fname = 'RandomQuote::selectRandomQuoteFromDB';
$res = $dbr->query( $sql, $fname );
return $dbr->fetchObject( $res );
}
Normally I use the $wgOut->addWikiText( $msg ); function.
I would modify your code to:
function wfGetQuote() {
global $wgOut; // Added line
$randstr = wfRandom();
$row = selectRandomQuoteFromDB( $randstr );
if( !$row )
$row = selectRandomQuoteFromDB( "0" );
if( $row ) {
list( $quote, $attribution ) = explode( "\n", $row->quote_text );
$wgOut->addWikiText( '<div id="trrandomquote"><div id="trquote">'. $quote .'</div> <span>'. $attribution .'</span></div>' );
}
else
return 'Error: No quote found';
}
I haven't tested it, but it may do the job you're looking for.

How to Do Facebook Login Using 'wp_insert_user'?

I am building a user generated content sharing theme and I want to make a custom Facebook connect. After researching, I found out this code.
The following code is adding the app to the users profile in Facebook, but is not creating a new user and also doesn't makes them logged in.
Here is the full code (to be added in functions.php).
Step 1 - Intializing javascript at header area
function fb_head(){
if( is_user_logged_in() )
return;
?>
<script type="text/javascript">
window.fbAsyncInit = function(){
FB.init({
appId:'APP_ID',
status:true,
cookie:true,
xfbml:true,
oauth:true
});
};
</script>
<div id="fb-root"></div>
<script type="text/javascript">
(function() {
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
<?php
}
add_action( 'wp_head', 'fb_head' );
Step 2 - Inserting a button for facebook login
<button id="facebook_connect">Connect with facebook</button>
Step 3 - Loading Jquery Library
function mytheme_enqueue_scripts(){
wp_enqueue_script( 'jquery' );
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_scripts');
Step 4 - Add a jQuery on-click function to the button we have created
This code will be placed on the wp footer section (before the closing body tag).
function fb_footer(){
if( is_user_logged_in()):
echo "<script type='text/javascript'> jQuery('#facebook_connect').hide(); </script>";
return;
endif;
?>
<script type="text/javascript">
jQuery('#facebook_connect').click(function(){
FB.login(function(FB_response){
if( FB_response.status === 'connected' ){
fb_intialize(FB_response);
}
},
{scope: 'email'});
});
function fb_intialize(FB_response){
FB.api(
'/me',
'GET',
{'fields':'id,email,username,verified,name'},
function(FB_userdata){
jQuery.ajax({
type: 'POST',
url: 'AJAXURL',
data: {
"action": "fb_intialize",
"FB_userdata": FB_userdata,
"FB_response": FB_response
},
success: function(user){
if( user.error ){
alert( user.error );
}
else if( user.loggedin ){
window.location.reload();
}
}
});
}
);
};
</script>
<?php
}
add_action( 'wp_footer', 'fb_footer' );
Step 5 - MAIN STEP: Adding the handler function
function wp_ajax_fb_intialize(){
#error_reporting( 0 ); // Don't break the JSON result
header( 'Content-type: application/json' );
if( !isset( $_REQUEST['FB_response'] ) || !isset( $_REQUEST['FB_userdata'] ))
die( json_encode( array( 'error' => 'Authonication required.' )));
$FB_response = $_REQUEST['FB_response'];
$FB_userdata = $_REQUEST['FB_userdata'];
$FB_userid = (int) $FB_userdata['id'];
if( !$FB_userid )
die( json_encode( array( 'error' => 'Please connect your facebook account.' )));
global $wpdb;
$user_ID = $wpdb->get_var( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '_fbid' AND meta_value = '$FB_userid'" );
if( !$user_ID ){
$user_email = $FB_userdata['email'];
$user_ID = $wpdb->get_var( "SELECT ID FROM $wpdb->users WHERE user_email = '$user_email'" );
if( !$user_ID ){
if ( !get_option( 'users_can_register' ))
die( json_encode( array( 'error' => 'Registration is not open at this time. Please come back later..' )));
extract( $FB_userdata );
$display_name = $name;
$user_login = $username;
if( empty( $verified ) || !$verified )
die( json_encode( array( 'error' => 'Your facebook account is not verified. You hae to verify your account before proceed login or registering on this site.' )));
$user_email = $email;
if ( empty( $user_email ))
die( json_encode( array( 'error' => 'Please re-connect your facebook account as we couldnt find your email address..' )));
if( empty( $name ))
die( json_encode( array( 'error' => 'empty_name', 'We didnt find your name. Please complete your facebook account before proceeding..' )));
if( empty( $user_login ))
$user_login = sanitize_title_with_dashes( sanitize_user( $display_name, true ));
if ( username_exists( $user_login ))
$user_login = $user_login. time();
$user_pass = wp_generate_password( 12, false );
$userdata = compact( 'user_login', 'user_email', 'user_pass', 'display_name' );
$user_ID = wp_insert_user( $userdata );
if ( is_wp_error( $user_ID ))
die( json_encode( array( 'error' => $user_ID->get_error_message())));
update_user_meta( $user_ID, '_fbid', (int) $id );
}
else{
update_user_meta( $user_ID, '_fbid', (int) $FB_userdata['id'] );
}
}
wp_set_auth_cookie( $user_ID, false, false );
die( json_encode( array( 'loggedin' => true )));
}
add_action( 'wp_ajax_nopriv_fb_intialize', 'wp_ajax_fb_intialize' );
THis is how I add the Facebook button:
<button id="facebook_connect">Connect with Facebook</button>
If anyone is trying it please replace the App ID.
Thanks for the code! Worked like a charm. But there is one really important security thing I discovered!
In the console I just changed facebook variables before sending the ajax request and I could login with everyones fb account (if you know their facebook id). The next couple of lines also check the fb token and validates if the login really is valid!
So in your wp_ajax_fb_intialize function you should definetely add these couple of lines:
$FB_response = $_REQUEST['FB_response'];
$FB_userdata = $_REQUEST['FB_userdata'];
$FB_userid = (int) $FB_userdata['id'];
//NEW CODE INSERT - check if token is valid
$token = $FB_response['authResponse']['accessToken'];
$path = 'https://graph.facebook.com/me?access_token='.$token;
$content = #file_get_contents($path);
$fb_user = json_decode($content);
if ($fb_user->id != $FB_userdata['id'])
die( json_encode( array( 'error' => 'FB login error' )));
I used file_get_contents but it is also possible to use curl
Changed the ajax url from
url: 'AJAXURL',
to
url: '<?php echo admin_url('admin-ajax.php'); ?>',
One thing I noticed while testing was that if I delete a user from the wp_users table the user meta with the facebook id will still be there so you may need to check if the facebook id is associated with an invalid user id in your ajax callback e.g.
global $wpdb;
$user_ID = $wpdb->get_var( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '_fbid' AND meta_value = '$FB_userid'" );
// check if the user id is valid
if( false === ($check_user = get_userdata($user_ID)) )
{
$user_ID = false; // set to false to force create a new user
}
The rest of your code worked fine.