Some of my PHP string disappears when being passed into SQL - php

I've created a Wordpress plugin that detects when a post is published and creates a topic in a phpBB installation.
As part of the plugin, the Wordpress post's excerpt is passed to the phpBB SQL database as the post text, followed by a few line breaks and a 'read more' link that is a permalink to the Wordpress post.
The excerpt + link is created as follows:
$wparticle_excerpt = get_the_excerpt()."<br /><br />Click here to read the full article →";
Then it's escaped:
$wparticle_excerpt = $con->real_escape_string( $wparticle_excerpt );
And is then written into the SQL database using the following query:
$sql_write_post = "INSERT INTO phpbb_posts (
topic_id,
forum_id,
poster_id,
icon_id,
post_time,
post_approved,
post_reported,
enable_bbcode,
enable_smilies,
enable_magic_url,
enable_sig,
post_subject,
post_text,
post_checksum,
post_attachment,
post_postcount,
post_edit_time,
post_edit_user,
post_edit_count,
post_edit_locked
) VALUES (
/* topic_id */ $new_topic_id,
/* forum_id */ $forum_to_post_in,
/* poster_id */ $userid_to_assign,
/* icon_id */ 0,
/* post_time */ $date_to_assign,
/* post_approved */ 1,
/* post_reported */ 0,
/* enable_bbcode */ 0,
/* enable_smilies */ 0,
/* enable_magic_url */ 1,
/* enable_sig */ 0,
/* post_subject */ '$wparticle_title',
/* post_text */ '$wparticle_excerpt',
/* post_checksum */ '$wparticle_excerpt_md5',
/* post_attachment */ 0,
/* post_postcount */ 1,
/* post_edit_time */ 0,
/* post_edit_user */ 0,
/* post_edit_count */ 0,
/* post_edit_locked */ 0
)";
if (!mysqli_query($con,$sql_write_post)) { die ("Error while creating post: " . mysqli_error($con)); }
The problem is that when I check the output, all I get are the two line breaks and the read more link. The actual excerpt, the part created by get_the_excerpt(), is completely gone. All I get is this:
<br />
<br />
Click here to read the full article →
I've dumped the $wparticle_excerpt variable after escaping it, and I know it's good. It's just one long string.
If I put any text in front of the excerpt, that stays, as long as it's hard-coded in PHP. Then there's a blank space where the excerpt should be, then the line breaks. No matter what I do I can't keep the excerpt there, it just disappears somewhere.
I'm sure this is the most simple thing but I'm mostly self-taught and I have no idea how or why this is happening. Any help would be incredibly appreciated. Please let me know if I haven't explained something properly.
The phpbb_posts table I'm writing to is utf8_bin and the post_text field in there is mediumtext type and utf8_bin, if this is of any help.

Related

Get random posts through WordPress API

I am building a self-test project which can give 10 questions at one time from a question list. I want the 10 questions should be different every time I start the test. The front-end is React and the back-end is WordPress by using WordPress API.
Previously I used orderby=rand in the query by implementing a plug-in
<?php
/**
* Plugin Name: REST API - Post list randomize
* Description: Randomize the content list in REST API passing `orderby=rand` as parameter.
* Version: 1.0.0
* Author: Felipe Elia | Codeable
* Author URI: https://codeable.io/developers/felipe-elia?ref=qGTOJ
*/
/**
* Add `rand` as an option for orderby param in REST API.
* Hook to `rest_{$this->post_type}_collection_params` filter.
*
* #param array $query_params Accepted parameters.
* #return array
*/
function add_rand_orderby_rest_post_collection_params( $query_params ) {
$query_params['orderby']['enum'][] = 'rand';
return $query_params;
}
add_filter( 'rest_post_collection_params', 'add_rand_orderby_rest_post_collection_params' );
It worked perfectly until 2 weeks ago. Without modifying any code, it was just broken. I used Postman to test, such as http://localhost/wp/wp-json/wp/v2/questions?per_page=10&orderby=rand. The response is
"code": "rest_invalid_param",
"message": "Invalid parameter(s): orderby",
"data": {
"status": 400,
"params": {
"orderby": "orderby is not one of author, date, id, include, modified, parent, relevance, slug, include_slugs, title."
}
}
Two weeks ago if I used the same query, it could give me 10 random questions. It looks like the plug-in cannot add rand successfully as a parameter for orderby in WordPress like before.
BTW, the functionality of orderby=rand in WP isn't broken because if I manually add rand as a parameter in WP core code, the above query can work again.
Does anybody know what's wrong with the plug-in or some latest updates in WP causing the problem?
Another thing is I saw some articles mentioning ORDERBY = RAND() in MySQL will affect the performance severely when the database is large. So I wonder whether I should use orderby=rand in the query to get random questions or think about other ways to do the job. Does anybody have any suggestions for this performance issue? Thanks!
Found the answer. I need to make the first parameter rest_post_collection_params of add_filter function to the correspondent post type.
The original rest_post_collection_params is for the WP default posts type. Because I use ACF(Advanced Custom Fields, another plug-in to create customized post types) to create my own post types, such as books, I need to change the first parameter to rest_books_collection_params. If you have more customized post types, just create as many as add_filter functions for each of them.
Just have no ideas why I could use rest_post_collection_params for all my customized post types 3 weeks ago but not now. Anyway, since it is solved, don't bother. My project is more front-end oriented. WP is just storage.
BTW, probably someone has noticed that rest_post_collection_params is for the WP default post type posts. In the parameter it uses single form post for the plural form posts. This only works for the WP default post type. For customized types, if it is books, the parameter should be rest_books_collection_param; if questions, then rest_questions_collection_param. Keep the parameter exactly the same as the post type.
For Getting Unique Questions:
table: wp_question_filter_list
id ip_address question_list
1 1.21.23 1,2,3,4,5
2 1.21.24 1,4,6,7,8
3 1.21.25 4,5,6,8,9
function get_unique_questions($ip_address){
global $wpdb;
$table_name = $wpdb->prefix . 'question_filter_list';
$unique_id_list=$wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE ip_address=%d",$ip_address), ARRAY_A);
if (count($unique_id_list) > 0) {
$question_data=$unique_id_list[0];
$arr_question=array();
if(isset($question_data['question_list']) && $question_data['question_list']!=''){
$arr_old_question_id_list=explode(",",$question_data['question_list']);
}
$excludes = implode(',', $arr_old_question_id_list);
$arr_question_id_list=$wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE ip_address='".$ip_address."' AND id NOT IN('".$excludes."') "), ARRAY_A);
set_unique_questions($ip_address,$arr_question_id_list);
}
function set_unique_questions($ip_address,$arr_question_id_list){
global $wpdb;
$table_name = $wpdb->prefix . 'question_filter_list';
$unique_id_list=$wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE ip_address=%d",$ip_address), ARRAY_A);
if (count($unique_id_list) > 0) {
$question_data=$unique_id_list[0];
$arr_question=array();
if(isset($question_data['question_list']) && $question_data['question_list']!=''){
$arr_old_question_id_list=explode(",",$question_data['question_list']);
}
if(is_array($arr_question_id_list) && !empty($arr_question_id_list)){
foreach($arr_question_id_list as $single_question){
array_push($arr_question,$single_question);
}
}
$str_question_id_list=implode(",",$arr_question);
$wpdb->update($table_name, array(
'question_list' => $str_question_id_list
), array(
'ip_address' => $ip_address
));
}else{
$str_question_id_list=implode(",",$arr_question_id_list);
$data = array(
'ip_address' => $ip_address,
'question_list' => $str_question_id_list,
);
$wpdb->insert($table_name, $data);
}
$result = $wpdb->insert($table_name, $item);
}
$ip_address=$_SERVER['REMOTE_ADDR'];
$arr_question_list=get_unique_questions($ip_address);
echo "<pre>";
print_r($arr_question_list);

organic group drupal 7 views_get_views

Trying to use views_get_views on the organic groups view OG all user group content to programmatically access the view using php. I am unable to set the parameter properly. I would like to get an example of working call.
Thanks,
Olivier
Try This:
/* load view object */
$views = views_get_view('og_all_user_group_content');
/* set default display */
$views->set_display('default');
/* pass group id as argument value */
$group_id =''; //set your group id here
$views->set_arguments(array($group_id));
/* execute view */
$views->execute();
/* result object */
if (!empty($views->result)) {
if (count($views->result) > 0) {
foreach ($views->result as $result) {
//$result contain array of data.
}
}
}

mPDF setAutoMargin not working for first page

The following code works perfectly for the second, third etc pages, but not for the first one.
/*
* Encoding
* Size (A4, etc)
* Font-size
* Font-type
* margin_left
* margin_right
* margin_top
* margin_bottom
* margin_header
* margin_footer
* Orientation
*/
$this->mPDF = new mPDF('utf-8', 'A4', 9, 'freesans', 10, 10, 0, 25, 5, 4, 'P');
$this->mPDF->setAutoTopMargin = 'pad';
$html = '<sethtmlpageheader name="myheader" value="on" show-this-page="1"></sethtmlpageheader>';
In the first page, the body content overlap the header content, while on the other pages the body content doesn't overlap the header content and fits perfectly below the header.
I want the same behavior on the first page, how do I do it?
EDIT 1:
An image to illustrate the problem I'm facing
I found out this answer http://www.mpdf1.com/forum/discussion/621/margin-top-problems/p1 which basically reports my issue.
The solution is to add a page and delete it so the code calculates the margins and then add again the page.
$this->mPDF->WriteHTML($html);
$this->mPDF->page = 0;
$this->mPDF->state = 0;
unset($this->mPDF->pages[0]);

How do I add a hook for WHMCS for when a client pays their bill?

For the past few hours I've been trying to figure out how to add a hook to WHMCS. At first, I tried the PendingOrder hook, which never got called. Then I tried the AcceptOrder, which also seemed to never get called. Last, I tried the InvoidPaid hook, which also doesn't seem to be working. Am I doing something wrong?
I have enabled my hook in AddonModules, but it seems like I can't get any hooks to call (or I am using the wrong one)
What I'm trying to do is just run a PHP script that contains all of the custom fields that a user entered when they paid for an order.
How would I go about doing this?
My main module:
<?php
/**
* #package GSProv
* #author -
* #copyright -
* #license None
* #version $Id$
* #link -
*/
if (!defined("WHMCS"))
{
die("Can't access this file directly!");
}
add_hook("InvoicePaid", 1, "gsprov_hook_provision");
add_hook("InvoiceUnpaid", 1, "gsprov_hook_suspend");
function gsprov_hook_provision($params)
{
$time = time();
mysql_connect("localhost", "root", "password");
mysql_select_db("testdb");
mysql_query("INSERT INTO log VALUES ($time, 'Order paid for!'");
}
function gsprov_hook_suspend($params)
{
$time = time();
mysql_connect("localhost", "root", "password");
mysql_select_db("testdb");
mysql_query("INSERT INTO log VALUES ($time, 'Order not paid for :('");
}
?>
There is an error in your SQL, your queries are missing the close bracket.
mysql_query("INSERT INTO log VALUES ($time, 'Order paid for!'");
Should be
mysql_query("INSERT INTO log VALUES ($time, 'Order paid for!')");

error in creating more charts in looping - php

I'm trying to create country based charts. i'm using pchart for this, because it will create charts as image by default.
I've created a function which creates barchart. I'm selecting countries and its stats and passing it to the barchart function which creates a chart as a flat file.
When i pass a single a country stats it is creating chart. the problem is when i try to post more than one country, it is creating only one country image not more than that. i don't know where the issue exists? Help appreciated.
sample code is here:
<?php
$con=mysql_connect("localhost","root","");
$ln=mysql_select_db("conif_new",$con);
$qry="select country from chart group by country limit 2";
$cnlist=mysql_query($qry);
while($row=mysql_fetch_row($cnlist)){
$cn=$row[0];
$nqry="select server,count(*) from chart where country='$cn' group by server";
$dset=mysql_query($nqry);
$x="";
$xt="";
while($crow=mysql_fetch_row($dset)){
$x.=$crow[1].",";
$xt.=$crow[0].",";
}
$x=substr($x,0,strlen($x)-1);
$xt=substr($xt,0,strlen($xt)-1);
//echo "$x**$xt<br>";
if(barChart($x,$xt,$cn)){}
}
function barChart($x,$xt,$name){
$x=explode(",",$x);
$xt=explode(",",$xt);
/* CAT:Bar Chart */
/* pChart library inclusions */
include("class/pData.class.php");
include("class/pDraw.class.php");
include("class/pImage.class.php");
/* Create and populate the pData object */
$MyData = new pData();
$MyData->addPoints($x,"Server A");
$MyData->setAxisName(0,"Hits");
$MyData->addPoints($xt,"Months");
$MyData->setSerieDescription("Months","Month");
$MyData->setAbscissa("Months");
/* Create the pChart object */
$myPicture = new pImage(700,230,$MyData);
$myPicture->drawGradientArea(0,0,700,230,DIRECTION_VERTICAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,
"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>100));
$myPicture->drawGradientArea(0,0,700,230,DIRECTION_HORIZONTAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,
"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>20));
$myPicture->setFontProperties(array("FontName"=>"fonts/verdana.ttf","FontSize"=>9));
/* Draw the scale */
$myPicture->setGraphArea(50,30,680,200);
$myPicture->drawScale(array("CycleBackground"=>TRUE,"DrawSubTicks"=>TRUE,"GridR"=>0,"GridG"=>0,"GridB"=>0,
"GridAlpha"=>10));
/* Turn on shadow computing */
$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
/* Draw the chart */
$settings = array("Gradient"=>TRUE,"DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"DisplayR"=>255,
"DisplayG"=>255,"DisplayB"=>255,"DisplayShadow"=>TRUE,"Surrounding"=>10);
$myPicture->drawBarChart($settings);
/* Write the chart legend */
$myPicture->drawLegend(580,12,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));
/* Render the picture (choose the best way) */
//$myPicture->autoOutput("pictures/example.drawBarChart.shaded.png");
if($myPicture->render("C:/wamp/www/chart/".$name.".png"))
echo "true";
else
echo "false";
}
?>
I found an answer!!!, just noticed "include statements" inside barchart function. Removed it, Thats it!
I should've placed "include_once" instead of "include" or should've moved the "include" statements outside the function to make it work.

Categories