Using a Composer class in functions.php - wordpress

I'm trying to add a shortcode to my Wordpress site to pull a piece of data from a Google spreadsheet and drop it into a page. To do this, I'm trying to use Sheetsu. The php libraries for Sheetsu are managed through Composer.
I've got a working piece of standalone code, but when I drop it in functions.php, like this...
function do_sheetsu() {
require('vendor/autoload.php');
use Sheetsu\Sheetsu;
$sheetsu = new Sheetsu(['sheetId' => '8b297aa81110']);
$response = $sheetsu->search(['id' => '2.05.1']);
$collection = $response->getCollection();
echo $collection->get(0)->answer;
}
add_shortcode('sheetsu','do_sheetsu');
...it blanks my site. If I comment out the use Sheetsu\Sheetsu; line, my site comes back, but I get no output, and the error "PHP Fatal error: Class 'Sheetsu' not found" which I suppose makes perfectly good sense.
I know enough php to be able to break things, apparently, and my knowledge of Composer comes mostly from messing around with Flarum a little bit.
I'm sure I'm missing something obvious here, I'm guessing involving namespace declarations or something, but I can't put the pieces together.
I'm looking suspiciously at my composer.json file, as well—something doesn't seem right, but I'm not sure what to fix.
For the record, my composer.json, composer.lock, and vendor folder are in my theme folder with functions.php. My composer.json file looks like this:
{
"require": {
"emilianozublena/sheetsu-php": "^0.0.6"
}
}
and I'm not sure it should.
But what's more troubling is finding a way around that use Sheetsu\Sheetsu line that seems to totally break Wordpress...
Thanks for any help!

I assume here you have installed the package using command line running composer install or composer require emilianozublena/sheetsu-php.
You cannot use the use php keyword inside function. The use keyword must be declared in the outermost scope of a file (the global scope). Refer to this answer for more detail here
So, in this condition you can chain the namespace while instantiating your class. In our case new Sheetsu(['sheetId' => '8b297aa81110']) becomes new \Sheetsu\Sheetsu(['sheetId' => '8b297aa81110']);
Try this code below
function do_sheetsu() {
require('vendor/autoload.php');
$sheetsu = new \Sheetsu\Sheetsu(['sheetId' => '8b297aa81110']);
$response = $sheetsu->search(['id' => '2.05.1']);
$collection = $response->getCollection();
echo $collection->get(0)->answer;
}
add_shortcode('sheetsu','do_sheetsu');

Related

Wordpress CodeMirror.fromTextArea is not a function error

I am using code mirror editor from wordpress bundle in a plugin page. I have enqueued the following styles and scripts as per some research here.
wp_enqueue_style('wp-codemirror');
$cm_settings['codeEditor'] = wp_enqueue_code_editor(array('type' => 'text/css'));
$var = array(
'cm_settings' => $cm_settings
);
wp_localize_script('my-admin-script', 'my_var', $var);
Then in my js file, i have added the following line to make the textarea my_textarea to code editor.
wp.codeEditor.initialize($('#my_textarea'), my_var.cm_settings);
Everything works fine. I am not able to get value from the code mirror editor. I tried
var editor = CodeMirror.fromTextArea(document.getElementById("my_textarea"));
console.log(editor.getValue());
But i am getting an error saying CodeMirror.fromTextArea is not a function. I have been stuck at this the whole day. Any helps are appreciated.
The issue was solved on setting a variable,
var editor = wp.codeEditor.initialize($('#my_textarea'), my_var.cm_settings);
and accessing the varibale editor, you can do anything.
for eg: you can use editor.codemirror.getValue() to access the value. You don't need to use CodeMirror.fromTextArea here.

Twig's dump function returns a blank screen

I am using Twig's dump function in Symfony2. I have configured Symfony according to its instructions.
I have a page variable, and an orders array. dump works on page, but not orders. When I call it on orders, I get a white screen - no php errors or anything. I have no idea how to debug this.
Any ideas?
A little explanation
A white (blank) screen in this case means the PHP fatal error: Allowed memory size exhausted. During my investigation, I found that twig uses thevar_dump function while I have VarDumper component installed.
I think its made to work along in case the VarDumper component is not installed, but twig's dump() function covered in symfony's VarDumper component documentation like a complex solution, that's strange.
So, using VarDumper's dump() function instead of native var_dump() solves the memory problem (because VarDumper limits result dump collection to adequate amount). Also VarDumper's dump() give more convenient results - you can click on tree leafs to show/hide its content.
What exactly do you need to do
Install VarDumper component if not installed
Go to file: vendor/twig/twig/lib/Twig/Extension/Debug.php
Find twig_var_dump function
Replace all var_dump() calls to dump()
Delete/comment ob_start() + ob_get_clean() construction (which is needed
if you use var_dump() as it echoes data immideately, but dump() acting
more intelligent)
OR
copy + replace the entire function using this:
function twig_var_dump(Twig_Environment $env, $context)
{
if (!$env->isDebug()) {
return;
}
$count = func_num_args();
if (2 === $count) {
$vars = array();
foreach ($context as $key => $value) {
if (!$value instanceof Twig_Template) {
$vars[$key] = $value;
}
}
dump($vars);
} else {
for ($i = 2; $i < $count; $i++) {
dump(func_get_arg($i));
}
}
}
PS: Question's asked in 2013, but I hope it helps because I had this problem now.
My context:
"symfony/symfony": "2.5.*"
"symfony/var-dumper": "~2.6"
I had same issue, increasing the memory_limit up to 1Gb didn't help.
In my case I want to call the
{{ dump() }}
with no parameters (to see all the available vars in the context of the current template. In my case the following helped: https://stackoverflow.com/a/11500141/2166261 , though the memory_limit issue still persists)
Most likely you are hitting your PHP memory limit. Try increasing it in the php.ini
There is probably no single answer to this problem, as a range of things could cause it. As other people have suggested, increasing the memory limit for PHP may help. In my case this did not help, however, I was able to resolve the issue by installing xdebug.
So far, it seems then that a checklist for resolving this issue would look something like this:
Ensure that the latest version of xdebug is installed
If that doesn't resolve it, try increasing PHP memory limit
The Arkemlar answer works fine.
BUT
after that the twig dump() caused a RuntimeException in the functional test with phpunit in my case.

Enabling hook form alter module - not showing up in modules

Good Day,
I am doing a hook_form_alter and followed a tutorial where you create a directory in your /sites/all/modules directory and place the module.info and mymodule.module files within. I've called that directory mymodule so the path will be /sites/all/modules/mymodule.
The problem I'm having is trying to enable the module. It does not show up in the list of Modules when I check. It's clearly in my directory.
My module (my_module.module) looks like this and is going to be used to modify the form where you add the product (basically taking out the SKU, list price and commission):
<?php
// $Id$
/**
* #file
* Module to hold my customizations to Ubercart
*/
/**
* Implementation of hook_form_alter()
*/
function my_module_form_alter($form_id, &$form) {
if ($form_id == 'product_node_form') {
$form['base']['model']['#required'] = FALSE;
$form['base']['prices']['list_price']['#required'] = FALSE;
$form['base']['prices']['cost']['#required'] = FALSE;
$form['base']['prices']['sell_price']['#required'] = FALSE;
$form['base']['prices']['shippable']['#required'] = FALSE;
}
}
And the info file, my_module.info, looks like this:
; $Id$
name = My module
description = My customizations to Ubercart
package = Custom
core = 7.x
Why would it not show up in the modules list and how do I make Drupal see the module so I can enable it?
I don't have a category called Custom and nothing is showing up.
So far, i've gone into my php.ini file and put in safe_mode=off to see if that would help (because someone said this sometimes causes Drupal to ignore new modules for some reason) but that didn't help.
I created both files on the server so I don't think there would be any issues with something like linefeeds (I use GoDaddy's FTP File Manager and it comes with an editor.)
What could the problem be because I'm stumped?
Thanks for looking.
Make sure to name the .info and .module files with the same name.
E.g. mymodule.info and mymodule.module.
Note that, the name should be alpha-numeric (underscore is allowed) and starts with a letter.
In your hook implementation, replace the word hook with your module's name
function mymodule_form_alter($form, &$form_state, $form_id)
{
// code goes here...
}
I discovered something that works. After reading about people having issues with linefeeds and not knowing if this was creating the problem, I decided to try something.
I went to a module directory and found a .info file. In my case, I went to a directory that had Commerce in it and copied the Commerce.info file to my custom module's directory.
Then, what I did was take out the stuff that Drupal added after it installed the Commerce module (you can do the same for whatever .info file you copy from) and modified the information at the start, removing any dependency lines or anything that doesn't matter. In other words, make it like the .info file you are trying to use but DON'T COPY from the .info you've been trying to enable!
Save it this file (in my case, it's still commerce.info.)
Now delete the .info file you were trying to enable originally.
Rename the new .info file to the name you wanted (commerce.info => mymodule.info.)
Go to your Modules page and refresh it.
Voila...there it is. Enable and save configuration.

Drupal Views - How to return information about a view that is being rendered

I'm having trouble dealing with hooks for the Views module in Drupal. What I'm trying to do is determine which view is being rendered so that I can identify it and make changes to it. IOW, I don't know ahead of time which view I'm working on.
In the code below, I've replaced my actual module name with "MODULENAME".
In my .module file I have included a file MODULENAME.views.inc file with the following:
include_once ( dirname(__FILE__) . '/MODULENAME.views.inc');
In the .views.inc file, I have a MODULENAME_views_api function like this:
function MODULENAME_views_api() {
return array('api' => 2, 'path'=> drupal_get_path('module', 'MODULENAME'),
);
}
Those seem to work just fine. So, now I try to get down to business with an actual hook...
function MODULENAME_views_pre_render(&$view) {
$get_view_info = $view->name;
echo $get_view_info;
}
MODULENAME_views_pre_render();
The problem is this throws an error, "Missing argument 1 for MODULENAME_views_pre_render().
So, obviously it expects me to pass in an identifier of some sort to tell it which view I want. But that's the whole point of this function is to determine which view is being rendered. If I knew the answer to that, then I wouldn't need to call the function in the first place.
Am I missing something obvious? Is there a function call that I can use to return this identifier?
You hook into things by implementing hooks, so this part of your code is ok:
<?php
function MODULENAME_views_pre_render(&$view) {
$get_view_info = $view->name;
echo $get_view_info;
}
But this:
<?php
MODULENAME_views_pre_render();
Why? You're not generating a view, Views is. It's not your job to invoke the hook. You just implement it.
So, you need to make changes to the view? you do it right there:
<?php
function MODULENAME_views_pre_render(&$view) {
if ($view->name == 'TheViewIWantToModify') {
// Make some changes to the $view
}
}
And that's it.
Also, note that depending on the type of modifications you want to do, you might want to implement another hook instead of hook_views_pre_render(). Take a look at the docs/docs.php file that comes with Views (version 6.x-2.12 at least, I don't know which version you have, and BTW you should indicate this) and starting on like 538 you'll see a few hook_views_pre_ and hook_views_post_ type of hooks (that is, their descriptions, for you to know what each of them are good for), and then you can decide which one to implement in your module.

how to properly register nginx header filter?

I'm writing an nginx module.
From looking at other examples I'm registering my header filter in my modules postconfiguration hook:
static ngx_int_t
mod_py_postconfig(ngx_conf_t *cf)
{
ngx_http_next_header_filter = ngx_http_top_header_filter;
ngx_http_top_header_filter = mod_py_headers_filter;
return NGX_OK;
}
But the handler is never called. I've set a breakpoint in gdb on ngx_http_top_header_filter change and it seems my module's postconfig is called first, but then runs postconfig of the ngx_http_write_filter_module which overrides ngx_http_top_header_filter w/o storing the old value:
static ngx_int_t
ngx_http_write_filter_init(ngx_conf_t *cf)
{
ngx_http_top_body_filter = ngx_http_write_filter;
return NGX_OK;
}
it seems like it is designed to be the very last on called, so how come my module's postconfig is called first?
From what I can see the order of modules is set in objs/ngx_modules.c
I was able to fix the problem by manually reordering the modules there so that my module comes after ngx_http_header_filter_module, but this feels like an ugly hack, and also makes it hard to automate build process as ./configure overwrites this file each time.
OK, so I figured it out myself. Documenting it here in case anyone else will need it.
I was adding my module to the wrong list. The nginx module is configured through a 'config' file insed module's directory. My had the following line in it:
HTTP_MODULES="$HTTP_MODULES ngx_http_my_module_name"
I searched for HTTP_MODULES usage and found nginx/auto/modules script which actually builds ngx_modules.c file. It turns out there are several possible module lists used by nginx/auto/modules. I needed to add my module to the HTTP_AUX_FILTER_MODULES list like so:
HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_my_module_name"
This placed my module at the right place just after HTTP_HEADERS_FILTER_MODULE and fixed the problem.

Resources