Pressbooks Name Spaces

I’m a little confused about namespaces in Pressbooks. Working on a local Windows Xampp development environment, 3.9.9/WP 4.7.5 and only approved PB plugins. I have created a TinyMCE Drop cap button that creates a span around the 1st letter/s of a paragraph. I have tried to follow best coding practices, placing it in symbionts, and adding the css/scss to the relevant files (partials: _editor.scss/components: specials:_dropcap.scss).Everything is working fine including export.

However, my php file contains the namespace,I thought would be correct - Pressbooks\Symbionts; but unless I also create a symbionts folder in the Luther/main theme it cannot find the javascript file.Which namespace, to point to the plugin folder symbionts should I use?

I also found I had to enter the css in both the root theme style.css/scss files for the dropcap effect to display on both the editor and web reader - is this correct?

Thanks

Hi MobiApp121

Our use of Namespaces is a bit like PHP’s PSR-0 standard.

That means that if you are using namespace Pressbooks\Symbionts then the file should be in the /includes/symbionts/ folder.

There is no such folder in Pressbooks?

Try:

  • Not using a namespace if your file is in symbionts.
    • No autoload functionality so don’t forget to require whatever new PHP file you are adding.
  • Using the correct, folder mapped, namespace.

More info about Namespaces.

Hi Dac,

Thanks for that. I used the require method in an earlier version of PB but it seemed to complain when I tried it with 3.9.9. As I say it’s working well if a little inelegant. Incidentally namespace Pressbooks\Symbionts; points to /pressbooks-book/symbionts/mce-dropcap-button/ for me (yes I created a symbionts folder in the root theme as a result) , but then I have this function in my mce-dropcap-button.php:

function addDropcapButton( $plugin_array ) {

		$plugin_array['dropcapbutton'] = get_template_directory_uri() . '/symbionts/mce-dropcap-button/assets/js/dropcap-button.js';
		return $plugin_array;
	}

Is there anyway to avoid the approved plugin warning when I go to the upload new plugin page? With the current namespace method I entered it into the pb-utility.php file, like so:

function include_plugins() {
	$plugins = array(
		'custom-metadata/custom_metadata.php' => 1,
		'pressbooks-latex/pb-latex.php' => 1,
		'mce-dropcap-button/mce-dropcap-button.php' => 1,
	);

But I’m guessing there is more to it and dare say you will think me foolish?

I don’t quite understand what you are trying to do. Your initial question was about namespaces. If now your question is how to add a button to TinyMce then you should write a separate plugin please look at:

Adding code to Pressbooks, unless you are submitting a PR to github for everyone to use, is not the best idea. Your code will get wiped out on upgrades.

Cheers.

OK thanks, I’ve already added the button and it’s working and I fully intend to make it available for everyone. This is on a local development platform, so I’m trying things out for the moment. The problem with an TinyMce format plugin with PB is it has to parse through all stages up to export for ePub, mobi and PDf for it to be useful, this it does. A plugin is more difficult for me to achieve the same, this is why I am asking questions and I think I’ve been very clear with what is intended. But thanks anyway. Here it is working:


1 Like