Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Calinou committed Dec 3, 2020
0 parents commit 3841109
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.php]
indent_size = 4
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.3.0
hooks:
- id: fix-byte-order-marker
- id: end-of-file-fixer
- id: trailing-whitespace

- id: mixed-line-ending
args: [--fix=lf]
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

## 1.0.0 - 2020-12-03

- Initial release.

[Unreleased]: https://github.com/Calinou/q2a-prefill-fields/compare/v1.0.0...HEAD
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright © 2020 Hugo Locurcio and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Pre-fill form fields for Question2Answer

A Question2Answer plugin to pre-fill form fields on the *Ask a question* page
using GET parameters.

## Features

Supported fields are:

- `title`
- `content` **(only when not using a rich/WYSIWYG editor)**
- `tags`

Since the user can input arbitrary values as GET parameters, the values are
escaped using PHP's `htmlspecialchars()` function. You can use multiple tags by
separating each tag with a `+` or `%20` in the URL.

## Example

The following URL, when entered in the browser address bar:

```text
http://localhost:8000/index.php?qa=ask&title=Hello+world&content=something&tags=one%20two%20three
```

Will result in:

- **Title:** Hello world
- **Content:** something
- **Tags:** one two three *(as 3 separate tags, since Q2A separates tags with spaces)*

## Installation

- Clone this repository or
[download a ZIP archive](https://github.com/Calinou/q2a-prefill-fields/archive/master.zip).
- Move the extracted folder to the `qa-plugin/` folder of your Question2Answer
installation.
- Log in as administrator on your Question2Answer instance, navigate to
**Admin > Plugins** and make sure the **Prefill Fields** plugin is visible
and enabled.

## License

Copyright © 2020 Hugo Locurcio and contributors

Unless otherwise specified, files in this repository are licensed under the
MIT license. See [LICENSE.md](LICENSE.md) for more information.
40 changes: 40 additions & 0 deletions qa-plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
// Copyright © 2020 Hugo Locurcio and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/*
Plugin Name: Prefill Fields
Plugin URI: https://github.com/Calinou/q2a-prefill-fields
Plugin Description: Allows prefilling question fields (title, content and tags) using GET parameters
Plugin Version: 1.0.0
Plugin Date: 2020-12-03
Plugin Author: Hugo Locurcio
Plugin Author URI: https://hugo.pro/
Plugin License: MIT
Plugin Minimum Question2Answer Version: 1.8
Plugin Update Check URI: https://raw.githubusercontent.com/Calinou/q2a-prefill-fields/master/qa-plugin.php
*/

// Don't allow this page to be requested directly from browser.
if (!defined('QA_VERSION')) {
header('Location: ../../');
exit;
}

qa_register_plugin_layer('qa-prefill-fields-layer.php', 'Prefill Fields Layer');
57 changes: 57 additions & 0 deletions qa-prefill-fields-layer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
// Copyright © 2020 Hugo Locurcio and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

class qa_html_theme_layer extends qa_html_theme_base
{
/**
* Adds the required JavaScript code to prefill question fields on the "Ask
* a question" page.
*
* Supported GET parameters are `title`, `content` and `tags`.
* NOTE: `content` prefilling only works if a rich/WYSIWYG editor is *not* in use!
* Otherwise, the rich editor's loading will override the field's contents.
*/
public function head_script()
{
if ($this->template === 'ask') {
$this->content['script'][] = '<script>
window.addEventListener("DOMContentLoaded", () => {
const titleField = document.querySelector("input[name=\'title\']");
if (titleField) {
titleField.value = "' . htmlspecialchars($_GET['title']) . '";
}
const contentField = document.querySelector("input[name=\'content\']");
if (contentField) {
contentField.value = "' . htmlspecialchars($_GET['content']) . '";
}
const tagsField = document.querySelector("input[name=\'tags\']");
if (tagsField) {
tagsField.value = "' . htmlspecialchars($_GET['tags']) . '";
}
});
</script>';
}

parent::head_script();
}
}

0 comments on commit 3841109

Please sign in to comment.