PHP Switch for star rating in wordpress - switch-statement

Trying to set up a star rating system from a number in a quick and simple way in my wordpress site.
Here is my code:
<?php
$stars = $business['rating'];
settype($stars, "integer");
switch ($stars) {
case 0:
echo '<img src="' . echo esc_url( home_url( '/' ) ); . 'wp-content/themes/child-theme/images/stars0.png" />';
break;
case 1:
echo '<img src="' . echo esc_url( home_url( '/' ) ); . 'wp-content/themes/child-theme/images/stars1.png" />';
break;
case 2:
echo '<img src="' . echo esc_url( home_url( '/' ) ); . 'wp-content/themes/child-theme/images/stars2.png" />';
break;
case 3:
echo '<img src="' . echo esc_url( home_url( '/' ) ); . 'wp-content/themes/child-theme/images/stars3.png" />';
break;
case 4:
echo '<img src="' . echo esc_url( home_url( '/' ) ); . 'wp-content/themes/child-theme/images/stars4.png" />';
break;
case 5:
echo '<img src="' . echo esc_url( home_url( '/' ) ); . 'wp-content/themes/child-theme/images/stars5.png" />';
break;
}
?>
Seems like it should work but it just isn't coming up.

What happens when you run your code?
What are the possible values for $business['rating']?
If the possible values are all strings that do not begin with a number, then when $stars is converted to an integer, $stars will be converted to 0.
In this scenario, case 0 will always be matched.
Test out the theory by removing case 0 adding a
default:
echo "No case matched: $stars for $business['rating']".
You also cannot add another echo statement, before the esc_url() function, and semicolon after the function when concatenating:
echo '<img src="' . esc_url( home_url( '/' ) ) . 'wp-content/themes/child-theme/images/stars0.png" />';
That might be a syntax error causing your code not to work.

Related

Trying to create a shortcode to insert a react app

I need to integrate a react application inside a wordpress website. I created a custom plugin and a shortcode but the shortcode renders an empty div. It's my first wordpress plugin so I might be missing something.
My plugin directory looks like this:
/public_html/wp-content/plugins/PatSearch
- PatSearch.php
/public_html/wp-content/plugins/PatSearch/includes
- enqueue.php
- shortcode.php
/public_html/wp-content/plugins/PatSearch/widget
- Contains the root of the react application. It has been build and works when I visit the build directory
PatSearch.php
<?php
/**
* #wordpress-plugin
* Plugin Name: Ajout d'un app React pour la recherche
*/
defined( 'ABSPATH' ) or die( 'Direct script access diallowed.' );
define( 'PAT_WIDGET_PATH', plugin_dir_path( __FILE__ ) . '/widget' );
define( 'PAT_ASSET_MANIFEST', PAT_WIDGET_PATH . '/build/asset-manifest.json' );
define( 'PAT_INCLUDES', plugin_dir_path( __FILE__ ) . '/includes' );
require_once( PAT_INCLUDES . '/enqueue.php' );
require_once( PAT_INCLUDES . '/shortcode.php' );
enqueue.php
<?php
// This file enqueues scripts and styles
defined( 'ABSPATH' ) or die( 'Direct script access disallowed.' );
add_action( 'init', function() {
add_filter( 'script_loader_tag', function( $tag, $handle ) {
if ( ! preg_match( '/^pat-/', $handle ) ) { return $tag; }
return str_replace( ' src', ' async defer src', $tag );
}, 10, 2 );
add_action( 'wp_enqueue_scripts', function() {
$asset_manifest = json_decode( file_get_contents( PAT_ASSET_MANIFEST ), true )['files'];
if ( isset( $asset_manifest[ 'main.css' ] ) ) {
wp_enqueue_style( 'pat', get_site_url() . $asset_manifest[ 'main.css' ] );
}
wp_enqueue_script( 'pat-runtime', get_site_url() . $asset_manifest[ 'runtime~main.js' ], array(), null, true );
wp_enqueue_script( 'pat-main', get_site_url() . $asset_manifest[ 'main.js' ], array('pat-runtime'), null, true );
foreach ( $asset_manifest as $key => $value ) {
if ( preg_match( '#static/js/(.*)\.chunk\.js#', $key, $matches ) ) {
if ( $matches && is_array( $matches ) && count( $matches ) === 2 ) {
$name = "pat-" . preg_replace( '/[^A-Za-z0-9_]/', '-', $matches[1] );
wp_enqueue_script( $name, get_site_url() . $value, array( 'pat-main' ), null, true );
}
}
if ( preg_match( '#static/css/(.*)\.chunk\.css#', $key, $matches ) ) {
if ( $matches && is_array( $matches ) && count( $matches ) == 2 ) {
$name = "pat-" . preg_replace( '/[^A-Za-z0-9_]/', '-', $matches[1] );
wp_enqueue_style( $name, get_site_url() . $value, array( 'pat' ), null );
}
}
}
});
});
shortcode.php
<?php
// This file enqueues a shortcode.
defined( 'ABSPATH' ) or die( 'Direct script access disallowed.' );
add_shortcode( 'pat_widget', function( $atts ) {
$default_atts = array();
$args = shortcode_atts( $default_atts, $atts );
return "<div id='pat-root'></div>";
});
I created a page and added the shortcode [pat_widget] saved it and visited the page but the react application located at /wp-content/plugins/PatSearch/widget/build/ was not embed like I thought ... When I look at the source code of the page, I can only see an empty <div id='pat-root'></div>
I found what the problem was. The wordpress side of things was working fine ... I had to modify the React index.js file that was generated in the build.
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
// Commented this line
// ReactDOM.render(<App />, document.getElementById("root"));
// Added this to match my shortcode requirements
const target = document.getElementById('pat-root');
if (target) { ReactDOM.render(<App />, target); }

dompdf: base64 encoded SVGs aren't rendered correctly

I'm rendering a PDF in Symfony with dompdf.
allow_url_fopen is deactivated on my server. So I insert every image as base64-encoded.
$imagesPath = "/var/www/project/web/print/img/";
$images = array(
"###FRONTPAGE###" => $imagesPath . "company_frontpage_2016.svg",
"###LOGO###" => $imagesPath . "company_logo.svg"
);
foreach( $images as $image => $imageFile ) {
if ( is_readable( $imageFile ) ) {
$base64String = base64_encode( fread( fopen( $imageFile, "r" ), filesize( $imageFile ) ) );
$html = str_replace( $image, "data:image/svg+xml;base64," . $base64String, $html );
} else {
$html = str_replace( $image, "File nicht lesbar", $html );
}
}
The absolute server path didn't work for me. Does anyone know a better solution for this?
I'm happy that I see the SVG in the PDF now. But they aren't rendered correctly. Any idea why?

Template for MIGX MODx

I have some PHP foreach that i have moved to MIGX, now can somebody know how to make template for MIGX, here is my PHP
<?php
$i = 0;
$y = 0;
$active = 'active';
echo '<ol class="carousel-indicators">';
foreach(glob($dir) as $file) {
if ($i < 1) {
echo '<li data-target="#myCarousel" data-slide-to="' . $i . '" class="active"></li>';
$i = $i + 1;
}
else {
echo '
<li data-target="#myCarousel" data-slide-to="' . $i . '"></li>';
$i = $i + 1;
}
}
echo '</ol>';
echo '<div class="carousel-inner">';
foreach(glob($dir) as $file) {
$y = $y + 1;
if ($y == 1) {
echo '
<div class="' . $active . ' item">
<img class="img_book" src="' . $file . '" alt="">
</div>
';
}
else {
$active = 'not-active';
echo '
<div class="' . $active . ' item">
<img class="img_book" src="' . $file . '" alt="">
</div>
';
}
}
echo '</div>';
The MIGX Extra comes with a Snippet getImageList that will parse the values in the MIGX TV and return them based on a Chunk template that you specify.
For example, you could put this snippet call in your Template:
<div class="carousel-inner">
[[getImageList?
&tvname=`myMIGXtv`
&tpl=`myTplChunk`
]]
</div>
The snippet would return the values stored in the MIGX TV named myMIGXtv, in the currently requested Resource, and format the output based on the tpl Chunk names myTplChunk. The content of myTplChunk would be something like:
<div class="[[+idx:is=`1`:then=`active`:else=``]] item">
<img class="img_book" src="[[+file]]" alt="[[+another_migx_field]]">
</div>
The syntax of calling the [[+idx]] placeholder with a : after the tag name, invokes the MODX output modifier class, which supports conditionals.
Here are some reference materials for the methods described above:
Documentation for MIGX getImageList
Documentation for MODX
Output Modifiers
The right way and wrong way to use conditional
MODX Output Modifiers

Mozilla doesn't displays datepicker input

I am using this function to create a date input
public static function date($name, $value = null, $options = array()) {
$input = '<input type="date" name="' . $name . '" value="' . $value . '"';
foreach ($options as $key => $value) {
$input .= ' ' . $key . '="' . $value . '"';
}
$input .= '>';
return $input;
}
And then in my view
<div class="form-group">
{{ Form::label('sgs', Lang::get('messages.sgs').'*', array('class'=>'control-label col-lg-4')) }}
<div class="col-lg-8">
{{ Helpers\Helper::date('sgs', isset($v->sgs) ? $v->sgs : '' , array(
'class' => 'form-control'))
}}
</div>
</div>
In Google Chrome it works, and displays datepicker which is displayed by clicking at a arrow, the problem is with Mozilla Firefox which doesnt displays datepicker and just displays it as a simple input field. Why is happening this
The HTML5 DatePicker is not supported in Firefox. Therefore, you will need to use some custom DatePicker plugin. You can see this for the compatibility reference:
http://caniuse.com/#feat=input-datetime

Opencart SEO URL for product filters

I recently got a chance to work on Opencart and I got a custom-made theme (Pav foodstore).
I am trying to implement auto-seo URLs in my project. For doing so, I downloaded a very good extension from Navneet Singh for auto-seo URLs. The problem is, it doesn't come with SEO URLs for product filters. Tried a few modifications, but I can't seem to get it working right.
Following is the VQmod xml file code :
<?xml version="1.0" encoding="UTF-8"?>
<modification>
<id>CUSTOM CHANGES DONE BY NAVNEET SINGH</id>
<version>1</version>
<vqmver>1.0.0</vqmver>
<author>NAVNEET SINGH</author>
<!-- SEO URLS -->
<file error="abort" name="system/library/response.php">
<operation>
<search position="after"><![CDATA[
public function output() {
]]></search>
<add><![CDATA[
if ($this->output) {
// REPLACE URLS WITH SEO URLS
function my_repl($value) {
if( defined(ENT_COMPAT) && defined(ENT_HTML401) && defined(ENT_IGNORE) ) {
$val=str_replace(" ","-",strtolower(trim(strtr(html_entity_decode(strip_tags($value),ENT_COMPAT|ENT_HTML401|ENT_IGNORE,"UTF-8"),"`~!##$%^&*()-_=+\\|[{]};:'\",<.>/?"," "))));
return rawurlencode($val); }
elseif( defined(ENT_COMPAT) && defined(ENT_IGNORE) ) {
$val=str_replace(" ","-",strtolower(trim(strtr(html_entity_decode(strip_tags($value),ENT_COMPAT|ENT_IGNORE,"UTF-8"),"`~!##$%^&*()-_=+\\|[{]};:'\",<.>/?"," "))));
return rawurlencode($val); }
else {
$val=str_replace(" ","-",strtolower(trim(strtr(html_entity_decode(strip_tags($value),ENT_COMPAT,"UTF-8"),"`~!##$%^&*()-_=+\\|[{]};:'\",<.>/?"," "))));
return rawurlencode($val); }
}
function my_preg_replace($m1) {
if($m1[1]=='information/information') $p2 = 'i';
elseif($m1[1]=='product/category') $p2 = 'c';
elseif($m1[1]=='product/category' && $m1[2]=='filter') $p2 = 'f';
elseif($m1[1]=='product/manufacturer/info') $p2 = 'm';
elseif($m1[1]=='product/product' && $m1[2]=='product_id') $p2 = 'pp';
elseif($m1[1]=='product/product' && $m1[2]=='manufacturer_id') $p2 = 'pm';
elseif($m1[1]=='product/product' && $m1[2]=='path') $p2 = 'pc';
else return;
// IF IMG TAG, USE IMG ALT TEXT IN SEO URL; ELSE, USE ANCHOR TEXT IN SEO URL
if($p2=='i' || $p2=='c' || $p2=='m' || $p2=='pp') {
if( preg_match( '#<img.*alt="([^"]*)"#iUs', $m1[4], $m2 ) )
$u1 = my_repl($m2[1]);
else
$u1 = my_repl($m1[4]);
return $p2 . trim($m1[3]) . "-" . $u1 . '">' . $m1[4] . "</a>";
}
elseif($p2=='pc') {
if( preg_match( '#<img.*alt="([^"]*)"#iUs', $m1[5], $m2 ) )
$u1 = my_repl($m2[1]);
else
$u1 = my_repl($m1[5]);
return $p2 . trim($m1[4]) . "-c" . trim($m1[3]) . "-" . $u1 . '">' . $m1[5] . "</a>";
}
elseif($p2=='pm') {
if( preg_match( '#<img.*alt="([^"]*)"#iUs', $m1[5], $m2 ) )
$u1 = my_repl($m2[1]);
else
$u1 = my_repl($m1[5]);
return $p2 . trim($m1[4]) . "-m" . trim($m1[3]) . "-" . $u1 . '">' . $m1[5] . "</a>";
}
}
// PATTERNS TO SEARCH FOR SEO URL REPLACEMENT
$p1s = array(
"#index\.php\?route\=(product/category)&(path)=([0-9_]+?)\">(.*)</a>#iUs",
"#index\.php\?route\=(product/category)&(path)=([0-9_]+?)&filter=([0-9]+?)\">(.*)</a>#iUs",
"#index\.php\?route\=(product/manufacturer/info)&(manufacturer_id)=([0-9]+?)\">(.*)</a>#iUs",
"#index\.php\?route\=(information/information)&(information_id)=([0-9]+?)\">(.*)</a>#iUs",
"#index\.php\?route\=(product/product)&(product_id)=([0-9]+?)[^\"]*\">(.*)</a>#iUs",
"#index\.php\?route\=(product/product)&(path)=([0-9_]+?)&product_id=([0-9]+?)\">(.*)</a>#iUs",
"#index\.php\?route\=(product/product)&(manufacturer_id)=([0-9]+?)&product_id=([0-9]+?)\">(.*)</a>#iUs",
);
$this->output = preg_replace_callback( $p1s, "my_preg_replace", $this->output );
}
]]></add>
</operation>
</file>
And this is my .htaccess code block :
RewriteEngine On
RewriteRule ^i([0-9]+)-(.*)$ index.php?route=information/information&information_id=$1 [L]
RewriteRule ^c([0-9_]+)-(.*)$ index.php?route=product/category&path=$1 [L]
RewriteRule ^c([0-9_]+)-f([0-9_]+)-(.*)$ index.php?route=product/category&path=$2&filter=$1 [L]
RewriteRule ^m([0-9]+)-(.*)$ index.php?route=product/manufacturer/info&manufacturer_id=$1 [L]
RewriteRule ^pp([0-9]+)-(.*)$ index.php?route=product/product&product_id=$1 [L]
RewriteRule ^pm([0-9]+)-m([0-9]+)-(.*)$ index.php?route=product/product&manufacturer_id=$2&product_id=$1 [L]
RewriteRule ^pc([0-9]+)-c([0-9_]+)-(.*)$ index.php?route=product/product&path=$2&product_id=$1 [L]
Any help would be appreciated.

Resources